Rakefile 7.1 KB

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