Rakefile 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 :autofix do
  126. #check links
  127. files.each do |file|
  128. twee_source = File.read(file.source_file)
  129. twee_source = twee_source.lines.each_with_index.map do |line, lineno|
  130. line.chomp!
  131. line
  132. =begin
  133. line
  134. .scan(%r{\[\[(.+?)\](\[(.+?)\])?\]})
  135. .map { |x| x[0] }
  136. .each do |link|
  137. case link.scan('->').length
  138. when 0
  139. #link contains no ->, probably not a problem
  140. text = dest = link
  141. passages[title][:outbound_links] << \
  142. { passage: dest, source_line: "#{file.source_file}:#{lineno}" }
  143. when 1
  144. # no problem
  145. text, dest = link.split('->')
  146. passages[title][:outbound_links] << \
  147. { passage: dest, source_line: "#{file.source_file}:#{lineno}" }
  148. else
  149. puts "#{file.source_file}:#{lineno} link contains multiple -> definitely a problem".colorize(:red)
  150. next
  151. end
  152. if text.start_with?('\'')
  153. if !text.end_with?('\'')
  154. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) does not end with single quote despite starting with one".colorize(:red)
  155. next
  156. end
  157. text = text[1..-2]
  158. text
  159. .to_enum(:scan, '\'')
  160. .map {Regexp.last_match.offset(0)[0]}
  161. .each do |offset|
  162. if text[offset - 1] != '\\'
  163. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) contains unquoted single quote in body".colorize(:red)
  164. end
  165. end
  166. elsif text.start_with?('"')
  167. if !text.end_with?('"')
  168. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) does not end with double quote despite starting with one".colorize(:red)
  169. next
  170. end
  171. text = text[1..-2]
  172. text
  173. .to_enum(:scan, '"')
  174. .map {Regexp.last_match.offset(0)[0]}
  175. .each do |offset|
  176. if text[offset - 1] != '\\'
  177. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) contains unquoted double quote in body".colorize(:red)
  178. end
  179. end
  180. elsif text =~ /\A[\[\]'"\\]*\z/
  181. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) contains special characters but is not quoted with single or double quotes".colorize(:red)
  182. end
  183. end
  184. =end
  185. end.join("\n") + "\n"
  186. File.write(file.source_file, twee_source)
  187. end
  188. end
  189. task :lint do
  190. titles = Set.new
  191. passages = {}
  192. title = nil
  193. files.each do |file|
  194. twee_source = IO.read(file.source_file)
  195. twee_source.lines.each_with_index do |line, lineno|
  196. lineno += 1
  197. # check for duplicate
  198. if line.start_with?('::') && !line.start_with?('::StoryIncludes')
  199. title = line.match(/^:: *([^\[]*?) *(\[(.*?)\])? *(<(.*?)>)? *$/)[1]
  200. #puts title
  201. if title != 'Twee2Settings' && titles.include?(title)
  202. puts "#{file.source_file}:#{lineno} duplicate title #{title}".red
  203. end
  204. titles.add(title)
  205. passages[title] = {outbound_links: [], inbound_links: [], source_line: "#{file.source_file}:#{lineno}"}
  206. end
  207. # check links
  208. line
  209. .scan(%r{\[\[(.+?)\](\[(.+?)\])?\]})
  210. .map { |x| x[0] }
  211. .each do |link|
  212. case link.scan('->').length
  213. when 0
  214. #link contains no ->, probably not a problem
  215. text = dest = link
  216. passages[title][:outbound_links] << \
  217. { passage: dest, source_line: "#{file.source_file}:#{lineno}" }
  218. when 1
  219. # no problem
  220. text, dest = link.split('->')
  221. passages[title][:outbound_links] << \
  222. { passage: dest, source_line: "#{file.source_file}:#{lineno}" }
  223. else
  224. puts "#{file.source_file}:#{lineno} link contains multiple -> definitely a problem".colorize(:red)
  225. next
  226. end
  227. if text.start_with?('\'')
  228. if !text.end_with?('\'')
  229. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) does not end with single quote despite starting with one".colorize(:red)
  230. next
  231. end
  232. text = text[1..-2]
  233. text
  234. .to_enum(:scan, '\'')
  235. .map {Regexp.last_match.offset(0)[0]}
  236. .each do |offset|
  237. if text[offset - 1] != '\\'
  238. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) contains unquoted single quote in body".colorize(:red)
  239. end
  240. end
  241. elsif text.start_with?('"')
  242. if !text.end_with?('"')
  243. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) does not end with double quote despite starting with one".colorize(:red)
  244. next
  245. end
  246. text = text[1..-2]
  247. text
  248. .to_enum(:scan, '"')
  249. .map {Regexp.last_match.offset(0)[0]}
  250. .each do |offset|
  251. if text[offset - 1] != '\\'
  252. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) contains unquoted double quote in body".colorize(:red)
  253. end
  254. end
  255. elsif text =~ /\A[\[\]'"\\]*\z/
  256. puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) contains special characters but is not quoted with single or double quotes".colorize(:red)
  257. end
  258. end
  259. # check for default text
  260. if line.strip == 'Double-click this passage to edit it.'
  261. #puts "#{file.source_file}:#{lineno} has the default text \"Double-click this passage to edit it.\""
  262. end
  263. # check for TODO/FIXME
  264. if line.include?('TODO') || line.include?('FIXME')
  265. #puts "#{file.source_file}:#{lineno} has TODO/FIXME text: #{line}"
  266. end
  267. # check for <<include>> as string
  268. if line.include?('<<include') && !line.include?('[[')
  269. puts "#{file.source_file}:#{lineno} has an <<include>>, but no link"
  270. end
  271. end
  272. end
  273. passages.each do |title, passage|
  274. passage[:outbound_links].each do |link|
  275. if passages[link[:passage]]
  276. passages[link[:passage]][:inbound_links] << {passage: title, source_file: passage[:source_line]}
  277. else
  278. puts "#{link[:source_line]} (title=#{title}) links to non-existent passage #{link[:passage]}".red
  279. end
  280. end
  281. end
  282. passages.each do |title, passage|
  283. if passage[:inbound_links].empty? && title != 'Twee2Settings'
  284. puts "#{passage[:source_line]} (title=#{title}) has no inbound links"
  285. end
  286. end
  287. end