Rakefile 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 import_main: %w(bundle_install) do
  105. file_set = Set.new(files)
  106. base_excludes=%w[../stylesheet.tw2 ../storyjs.tw2 ../stylesheet.tw2 ../images.tw2]
  107. files.each do |file|
  108. other_files = (file_set - Set.new([file])).map { |f| File.basename(f.source_file) }
  109. excludes = other_files + base_excludes
  110. sh(
  111. 'bundle', 'exec', 'twee2', 'decompile',
  112. File.join(STORY_DIR, STORY_RELEASE_TITLE + '.html'),
  113. './' + file.source_file,
  114. '--exclude=StoryJS,StoryCSS,StoryTitle',
  115. "--exclude-from=#{excludes.join(',')}",
  116. )
  117. end
  118. end
  119. task :bundle_install do
  120. sh 'bundle check || bundle install'
  121. end
  122. task :clean do
  123. # TODO
  124. end
  125. task :lint do
  126. titles = Set.new
  127. passages = {}
  128. title = nil
  129. files.each do |file|
  130. twee_source = IO.read(file.source_file)
  131. twee_source.lines.each_with_index do |line, lineno|
  132. lineno += 1
  133. # check for duplicate
  134. if line.start_with?('::') && !line.start_with?('::StoryIncludes')
  135. title = line.match(/^:: *([^\[]*?) *(\[(.*?)\])? *(<(.*?)>)? *$/)[1]
  136. #puts title
  137. if title != 'Twee2Settings' && titles.include?(title)
  138. puts "#{file.source_file}:#{lineno} duplicate title #{title}".red
  139. end
  140. titles.add(title)
  141. passages[title] = {outbound_links: [], inbound_links: [], source_line: "#{file.source_file}:#{lineno}"}
  142. end
  143. # check links
  144. line
  145. .scan(%r{\[\[(.+?)\](\[(.+?)\])?\]})
  146. .map { |x| x[0] }
  147. .each do |link|
  148. case link.scan('->').length
  149. when 0
  150. #link contains no ->, probably not a problem
  151. text = dest = link
  152. passages[title][:outbound_links] << \
  153. { passage: dest, source_line: "#{file.source_file}:#{lineno}" }
  154. when 1
  155. # no problem
  156. text, dest = link.split('->')
  157. passages[title][:outbound_links] << \
  158. { passage: dest, source_line: "#{file.source_file}:#{lineno}" }
  159. else
  160. puts "#{file.source_file}:#{lineno} link contains multiple -> definitely a problem".colorize(:red)
  161. next
  162. end
  163. if text.start_with?('\'')
  164. if !text.end_with?('\'')
  165. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) does not end with single quote despite starting with one".colorize(:red)
  166. next
  167. end
  168. text = text[1..-2]
  169. text
  170. .to_enum(:scan, '\'')
  171. .map {Regexp.last_match.offset(0)[0]}
  172. .each do |offset|
  173. if text[offset - 1] != '\\'
  174. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) contains unquoted single quote in body".colorize(:red)
  175. end
  176. end
  177. elsif text.start_with?('"')
  178. if !text.end_with?('"')
  179. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) does not end with double quote despite starting with one".colorize(:red)
  180. next
  181. end
  182. text = text[1..-2]
  183. text
  184. .to_enum(:scan, '"')
  185. .map {Regexp.last_match.offset(0)[0]}
  186. .each do |offset|
  187. if text[offset - 1] != '\\'
  188. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) contains unquoted double quote in body".colorize(:red)
  189. end
  190. end
  191. elsif text =~ /\A[\[\]'"\\]*\z/
  192. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) contains special characters but is not quoted with single or double quotes".colorize(:red)
  193. end
  194. end
  195. # check for default text
  196. if line.strip == 'Double-click this passage to edit it.'
  197. #puts "#{file.source_file}:#{lineno} has the default text \"Double-click this passage to edit it.\""
  198. end
  199. # check for TODO/FIXME
  200. if line.include?('TODO') || line.include?('FIXME')
  201. #puts "#{file.source_file}:#{lineno} has TODO/FIXME text: #{line}"
  202. end
  203. # check for <<include>> as string
  204. if line.include?('<<include') && !line.include?('[[')
  205. puts "#{file.source_file}:#{lineno} has an <<include>>, but no link"
  206. end
  207. end
  208. end
  209. passages.each do |title, passage|
  210. passage[:outbound_links].each do |link|
  211. if passages[link[:passage]]
  212. passages[link[:passage]][:inbound_links] << {passage: title, source_file: passage[:source_line]}
  213. else
  214. puts "#{link[:source_line]} (title=#{title}) links to non-existent passage #{link[:passage]}".red
  215. end
  216. end
  217. end
  218. passages.each do |title, passage|
  219. if passage[:inbound_links].empty? && title != 'Twee2Settings'
  220. puts "#{passage[:source_line]} (title=#{title}) has no inbound links"
  221. end
  222. end
  223. end