Rakefile 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. task default: %w(develop)
  2. STORY_DIR=File.join(ENV['USERPROFILE'], 'Documents\\Twine\\Stories')
  3. STORY_TITLE = 'Volleyball'
  4. STORY_FORMAT = 'SugarCube'
  5. STORY_FORMAT_VERSION = '2.25.0'
  6. FILES = {
  7. '01_intro' => '01 Intro',
  8. '02_transform' => '02 Transform',
  9. '03_custom_girl' => '03 Custom Girl',
  10. '04_money_game' => '04 Money Games',
  11. '05_beaches' => '05 Beaches',
  12. }
  13. def files
  14. FILES.map do |short_name, long_name|
  15. OpenStruct.new({
  16. short_name: short_name,
  17. long_name: long_name,
  18. source_file: "story/#{short_name}.tw2",
  19. dest_file: File.join(STORY_DIR, "#{STORY_TITLE} - #{long_name}.html"),
  20. wrapper_file: "#{short_name}_wrapper.tw2",
  21. })
  22. end
  23. end
  24. task develop: %w(bundle_install) do
  25. sh "bundle exec twee2 build main.tw2 develop.html"
  26. end
  27. task watch: %w(bundle_install) do
  28. sh 'start watch.html' # FIXME non-windows
  29. sh "bundle exec twee2 watch main.tw2 watch.html"
  30. end
  31. task release: %w(bundle_install) do
  32. sh "bundle exec twee2 build main.tw2 release.html"
  33. end
  34. desc 'export sub files to Twine 2'
  35. task export: %w(bundle_install) do
  36. files.each do |file|
  37. IO.write file.wrapper_file, <<~EOF
  38. ::StoryTitle
  39. Volleyball - #{file.long_name}
  40. ::Twee2Settings [twee2]
  41. @story_start_name = 'Start #{file.long_name}'
  42. Twee2::build_config.story_format = 'SugarCube'
  43. Twee2::build_config.story_format_version = 'SugarCube2'
  44. ::StoryIncludes
  45. images.tw2
  46. stylesheet.tw2
  47. storyjs.tw2
  48. #{file.source_file}
  49. EOF
  50. sh(
  51. 'bundle', 'exec', 'twee2', 'export',
  52. "--format=#{STORY_FORMAT}", "--format-version=#{STORY_FORMAT_VERSION}",
  53. file.wrapper_file, file.dest_file,
  54. )
  55. end
  56. sh(
  57. 'bundle', 'exec', 'twee2', 'export',
  58. "--format=#{STORY_FORMAT}", "--format-version=#{STORY_FORMAT_VERSION}",
  59. 'main.tw2', STORY_DIR,
  60. )
  61. end
  62. desc 'import sub files from Twine 2'
  63. task import: %w(bundle_install) do
  64. files.each do |file|
  65. sh(
  66. 'bundle', 'exec', 'twee2', 'decompile',
  67. file.dest_file, file.source_file,
  68. '--exclude=StoryJS,StoryCSS,StoryTitle',
  69. '--exclude-from=../stylesheet.tw2,../storyjs.tw2,../stylesheet.tw2,../images.tw2',
  70. )
  71. end
  72. end
  73. task :bundle_install do
  74. sh 'bundle install'
  75. end
  76. task :clean do
  77. # TODO
  78. end
  79. task :lint do
  80. FILES.each do |short_name, long_name|
  81. filename = "#{short_name}.tw2"
  82. twee_source = IO.read(filename)
  83. twee_source.lines.each_with_index do |line, lineno|
  84. lineno += 1
  85. line
  86. .scan(%r{\[\[([^\]]*)\]\]})
  87. .map { |x| x[0] }
  88. .each do |link|
  89. case link.scan('->').length
  90. when 0
  91. #puts("link contains no ->, probably not a problem")
  92. text = link
  93. when 1
  94. # no problem
  95. text, dest = link.split('->')
  96. else
  97. puts("link contains multiple -> definitely a problem")
  98. next
  99. end
  100. if text.start_with?('\'')
  101. if !text.end_with?('\'')
  102. puts("#{filename}:#{lineno} link text (#{text.inspect}) does not end with single quote despite starting with one")
  103. next
  104. end
  105. text = text[1..-2]
  106. text
  107. .to_enum(:scan, '\'')
  108. .map {Regexp.last_match.offset(0)[0]}
  109. .each do |offset|
  110. if text[offset - 1] != '\\'
  111. puts "#{filename}:#{lineno} link text (#{text.inspect}) contains unquoted single quote in body"
  112. end
  113. end
  114. elsif text.start_with?('"')
  115. if !text.end_with?('"')
  116. puts("#{filename}:#{lineno} link text (#{text.inspect}) does not end with double quote despite starting with one")
  117. next
  118. end
  119. text = text[1..-2]
  120. text
  121. .to_enum(:scan, '"')
  122. .map {Regexp.last_match.offset(0)[0]}
  123. .each do |offset|
  124. if text[offset - 1] != '\\'
  125. puts "#{filename}:#{lineno} link text (#{text.inspect}) contains unquoted double quote in body"
  126. end
  127. end
  128. elsif text =~ /\A[\[\]'"\\]*\Z/
  129. puts "#{filename}:#{lineno} link text (#{text.inspect}) contains special characters but is not quoted with single or double quotes"
  130. end
  131. end
  132. end
  133. end
  134. end