Rakefile 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. task default: %w(develop)
  2. begin
  3. require 'colorize'
  4. String.disable_colorization = false
  5. rescue LoadError
  6. end
  7. require 'set'
  8. STORY_DIR=File.join(ENV['USERPROFILE'], 'Documents\\Twine\\Stories')
  9. STORY_TITLE = 'Volleyball'
  10. STORY_FORMAT = 'SugarCube'
  11. STORY_FORMAT_PATH = "story_formats/sugarcube-2.28.2-for-twine-2.1-local/sugarcube-2"
  12. STORY_FORMAT_VERSION = '2.28.2'
  13. STORY_RELEASE_TITLE = 'Volleyball Chapter 1 v0.5'
  14. FILES = {
  15. '01_intro' => '01 Intro',
  16. '02_transform' => '02 Transform',
  17. '03_custom_girl' => '03 Custom Girl',
  18. '04_money_game' => '04 Money Games',
  19. '05_beaches' => '05 Beaches',
  20. '06_public_beach' => '06 Public Beach',
  21. }
  22. def files
  23. FILES.map do |short_name, long_name|
  24. OpenStruct.new({
  25. short_name: short_name,
  26. long_name: long_name,
  27. source_file: "story/#{short_name}.tw2",
  28. dest_file: File.join(STORY_DIR, "#{STORY_TITLE} - #{long_name}.html"),
  29. wrapper_file: "#{short_name}_wrapper.tw2",
  30. })
  31. end
  32. end
  33. task develop: %w(bundle_install storyjs story.tw2 story_title.tw2) do
  34. sh "bundle exec twee2 build '--format=#{STORY_FORMAT_PATH}' main.tw2 develop.html"
  35. end
  36. task watch: %w(bundle_install storyjs story.tw2 story_title.tw2) do
  37. sh 'start watch.html' # FIXME non-windows
  38. sh "bundle exec twee2 watch '--format=#{STORY_FORMAT_PATH}' main.tw2 watch.html"
  39. end
  40. task release: %w(bundle_install storyjs story.tw2 story_title.tw2) do
  41. sh "bundle exec twee2 build '--format=#{STORY_FORMAT_PATH}' main.tw2 release.html"
  42. end
  43. task :storyjs do
  44. File.write(
  45. 'storyjs.tw2',
  46. File.read('storyjs.tw2')\
  47. .sub(/\$STORY_FORMAT_VERSION = '[^']*'/, "$STORY_FORMAT_VERSION = '#{STORY_FORMAT_VERSION}'")
  48. )
  49. end
  50. file 'story.tw2' do
  51. File.open('story.tw2', 'w') do |f|
  52. f.puts('::StoryIncludes')
  53. files.each do |file|
  54. f.puts(file.source_file)
  55. end
  56. end
  57. end
  58. file 'story_title.tw2' do
  59. File.open('story_title.tw2', 'w') do |f|
  60. f.puts('::StoryTitle')
  61. f.puts("#{STORY_RELEASE_TITLE}")
  62. end
  63. end
  64. desc 'export sub files to Twine 2'
  65. task export: %w(bundle_install storyjs story.tw2) do
  66. files.each do |file|
  67. IO.write file.wrapper_file, <<~EOF
  68. ::StoryTitle
  69. Volleyball - #{file.long_name}
  70. ::Twee2Settings [twee2]
  71. @story_start_name = 'Start #{file.long_name}'
  72. Twee2::build_config.story_format = '#{STORY_FORMAT}'
  73. Twee2::build_config.story_format_version = '#{STORY_FORMAT_VERSION}'
  74. Twee2::build_config.story_ifid = ''
  75. ::StoryIncludes
  76. images.tw2
  77. stylesheet.tw2
  78. storyjs.tw2
  79. #{file.source_file}
  80. EOF
  81. sh(
  82. 'bundle', 'exec', 'twee2', 'export',
  83. "--format=#{STORY_FORMAT}", "--format-version=#{STORY_FORMAT_VERSION}",
  84. file.wrapper_file, file.dest_file,
  85. )
  86. end
  87. sh(
  88. 'bundle', 'exec', 'twee2', 'export',
  89. "--format=#{STORY_FORMAT}", "--format-version=#{STORY_FORMAT_VERSION}",
  90. 'main.tw2', STORY_DIR,
  91. )
  92. end
  93. desc 'import sub files from Twine 2'
  94. task import: %w(bundle_install) do
  95. files.each do |file|
  96. sh(
  97. 'bundle', 'exec', 'twee2', 'decompile',
  98. file.dest_file, file.source_file,
  99. '--exclude=StoryJS,StoryCSS,StoryTitle',
  100. '--exclude-from=../stylesheet.tw2,../storyjs.tw2,../stylesheet.tw2,../images.tw2',
  101. )
  102. end
  103. end
  104. task :bundle_install do
  105. sh 'bundle check || bundle install'
  106. end
  107. task :clean do
  108. # TODO
  109. end
  110. task :lint do
  111. titles = Set.new
  112. passages = {}
  113. title = nil
  114. files.each do |file|
  115. twee_source = IO.read(file.source_file)
  116. twee_source.lines.each_with_index do |line, lineno|
  117. lineno += 1
  118. # check for duplicate
  119. if line.start_with?('::') && !line.start_with?('::StoryIncludes')
  120. title = line.match(/^:: *([^\[]*?) *(\[(.*?)\])? *(<(.*?)>)? *$/)[1]
  121. #puts title
  122. if title != 'Twee2Settings' && titles.include?(title)
  123. puts "#{file.source_file}:#{lineno} duplicate title #{title}".red
  124. end
  125. titles.add(title)
  126. passages[title] = {outbound_links: [], inbound_links: [], source_line: "#{file.source_file}:#{lineno}"}
  127. end
  128. # check links
  129. line
  130. .scan(%r{\[\[(.+?)\](\[(.+?)\])?\]})
  131. .map { |x| x[0] }
  132. .each do |link|
  133. case link.scan('->').length
  134. when 0
  135. #link contains no ->, probably not a problem
  136. text = dest = link
  137. passages[title][:outbound_links] << \
  138. { passage: dest, source_line: "#{file.source_file}:#{lineno}" }
  139. when 1
  140. # no problem
  141. text, dest = link.split('->')
  142. passages[title][:outbound_links] << \
  143. { passage: dest, source_line: "#{file.source_file}:#{lineno}" }
  144. else
  145. puts "#{file.source_file}:#{lineno} link contains multiple -> definitely a problem".colorize(:red)
  146. next
  147. end
  148. if text.start_with?('\'')
  149. if !text.end_with?('\'')
  150. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) does not end with single quote despite starting with one".colorize(:red)
  151. next
  152. end
  153. text = text[1..-2]
  154. text
  155. .to_enum(:scan, '\'')
  156. .map {Regexp.last_match.offset(0)[0]}
  157. .each do |offset|
  158. if text[offset - 1] != '\\'
  159. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) contains unquoted single quote in body".colorize(:red)
  160. end
  161. end
  162. elsif text.start_with?('"')
  163. if !text.end_with?('"')
  164. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) does not end with double quote despite starting with one".colorize(:red)
  165. next
  166. end
  167. text = text[1..-2]
  168. text
  169. .to_enum(:scan, '"')
  170. .map {Regexp.last_match.offset(0)[0]}
  171. .each do |offset|
  172. if text[offset - 1] != '\\'
  173. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) contains unquoted double quote in body".colorize(:red)
  174. end
  175. end
  176. elsif text =~ /\A[\[\]'"\\]*\z/
  177. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) contains special characters but is not quoted with single or double quotes".colorize(:red)
  178. end
  179. end
  180. # check for default text
  181. if line.strip == 'Double-click this passage to edit it.'
  182. #puts "#{file.source_file}:#{lineno} has the default text \"Double-click this passage to edit it.\""
  183. end
  184. # check for TODO/FIXME
  185. if line.include?('TODO') || line.include?('FIXME')
  186. #puts "#{file.source_file}:#{lineno} has TODO/FIXME text: #{line}"
  187. end
  188. # check for <<include>> as string
  189. if line.include?('<<include') && !line.include?('[[')
  190. puts "#{file.source_file}:#{lineno} has an <<include>>, but no link"
  191. end
  192. end
  193. end
  194. passages.each do |title, passage|
  195. passage[:outbound_links].each do |link|
  196. if passages[link[:passage]]
  197. passages[link[:passage]][:inbound_links] << {passage: title, source_file: passage[:source_line]}
  198. else
  199. puts "#{link[:source_line]} (title=#{title}) links to non-existent passage #{link[:passage]}".red
  200. end
  201. end
  202. end
  203. passages.each do |title, passage|
  204. if passage[:inbound_links].empty? && title != 'Twee2Settings'
  205. puts "#{passage[:source_line]} (title=#{title}) has no inbound links"
  206. end
  207. end
  208. end