Rakefile 7.7 KB

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