|
| 1 | +require 'asciidoctor' |
| 2 | +require 'pandoc-ruby' |
| 3 | +require 'pathname' |
| 4 | +require 'rake' |
| 5 | +require 'yaml' |
| 6 | + |
| 7 | +BUILD_FILENAME = '_build_cfg.yml' |
| 8 | +BUILDER_DIRNAME = '_build_system' |
| 9 | +PREVIEW_DIRNAME = '_preview' |
| 10 | +PACKAGE_DIRNAME = '_package' |
| 11 | +BLANK_STRING_RE = Regexp.new('^\s*$') |
| 12 | + |
| 13 | +def source_dir |
| 14 | + @source_dir ||= File.expand_path('..',Dir.pwd) |
| 15 | +end |
| 16 | + |
| 17 | +def builder_dir |
| 18 | + @builder_dir = File.join(source_dir,BUILDER_DIRNAME) |
| 19 | +end |
| 20 | + |
| 21 | +def builder_template_dir |
| 22 | + @builder_template_dir ||= File.join(builder_dir,'templates') |
| 23 | +end |
| 24 | + |
| 25 | +def preview_dir |
| 26 | + @preview_dir ||= begin |
| 27 | + preview_dir = File.join(source_dir,PREVIEW_DIRNAME) |
| 28 | + if not File.exists?(preview_dir) |
| 29 | + Dir.mkdir(preview_dir) |
| 30 | + end |
| 31 | + preview_dir |
| 32 | + end |
| 33 | +end |
| 34 | + |
| 35 | +def package_dir |
| 36 | + @package_dir ||= begin |
| 37 | + package_dir = File.join(source_dir,PACKAGE_DIRNAME) |
| 38 | + if not File.exists?(package_dir) |
| 39 | + Dir.mkdir(package_dir) |
| 40 | + end |
| 41 | + package_dir |
| 42 | + end |
| 43 | +end |
| 44 | + |
| 45 | +def build_config_file |
| 46 | + @build_config_file ||= File.join(source_dir,BUILD_FILENAME) |
| 47 | +end |
| 48 | + |
| 49 | +def build_config |
| 50 | + @build_config ||= validate_config(YAML.load_stream(open(build_config_file))) |
| 51 | +end |
| 52 | + |
| 53 | +def validate_config config_data |
| 54 | + # Validate/normalize the config file straight away |
| 55 | + if not config_data.is_a?(Array) |
| 56 | + raise "The configutaration in #{build_config_file} is malformed; the build system is expecting an array of topic groups." |
| 57 | + end |
| 58 | + config_data.each do |topic_group| |
| 59 | + # Check for presence of topic group keys |
| 60 | + ['Name','Dir','Topics'].each do |group_key| |
| 61 | + if not topic_group.has_key?(group_key) |
| 62 | + raise "One of the topic groups in #{build_config_file} is missing the '#{group_key}' key." |
| 63 | + end |
| 64 | + end |
| 65 | + # Check for right format of topic group values |
| 66 | + ['Name','Dir'].each do |group_key| |
| 67 | + if not topic_group[group_key].is_a?(String) |
| 68 | + raise "One of the topic groups in #{build_config_file} is not using a string for the #{group_key} setting; current value is #{topic_group[group_key].inspect}" |
| 69 | + end |
| 70 | + if topic_group[group_key].empty? or topic_group[group_key].match BLANK_STRING_RE |
| 71 | + raise "One of the topic groups in #{build_config_file} is using a blank value for the #{group_key} setting." |
| 72 | + end |
| 73 | + end |
| 74 | + if not File.exists?(File.join(source_dir,topic_group['Dir'])) |
| 75 | + raise "In #{build_config_file}, the directory #{topic_group['Dir']} for topic group #{topic_group['Name']} does not exist under #{source_dir}" |
| 76 | + end |
| 77 | + if not topic_group['Topics'].is_a?(Array) |
| 78 | + raise "The #{topic_group['Name']} topic group in #{build_config_file} is malformed; the build system is expecting an array of 'Topic' definitions." |
| 79 | + end |
| 80 | + # Now buzz through the topics |
| 81 | + topic_group['Topics'].each do |topic| |
| 82 | + ['Name','File'].each do |topic_key| |
| 83 | + if not topic[topic_key].is_a?(String) |
| 84 | + raise "In #{build_config_file}, topic group #{topic_group['Name']}, one of the topics is not using a string for the '#{topic_key}' setting; current value is #{topic[topic_key].inspect}" |
| 85 | + end |
| 86 | + if topic[topic_key].empty? or topic[topic_key].match BLANK_STRING_RE |
| 87 | + raise "In #{build_config_file}, topic group #{topic_group['Name']}, one of the topics is using a blank value for the '#{topic_key}' setting" |
| 88 | + end |
| 89 | + end |
| 90 | + # Normalize the filenames |
| 91 | + if topic['File'].end_with?('.adoc') |
| 92 | + topic['File'] = topic['File'][0..-6] |
| 93 | + end |
| 94 | + if not File.exists?(File.join(source_dir,topic_group['Dir'],"#{topic['File']}.adoc")) |
| 95 | + raise "In #{build_config_file}, could not find file #{topic['File']} under directory #{topic_group['Dir']} for topic #{topic['Name']} in topic group #{topic_group['Name']}." |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | + config_data |
| 100 | +end |
| 101 | + |
| 102 | +def nav_tree |
| 103 | + @nav_tree ||= begin |
| 104 | + navigation = [] |
| 105 | + build_config.each do |topic_group| |
| 106 | + topic_list = [] |
| 107 | + topic_group['Topics'].each do |topic| |
| 108 | + topic_list << ["#{topic_group['Dir']}/#{topic['File']}.html",topic['Name']] |
| 109 | + end |
| 110 | + navigation << { :title => topic_group['Name'], :topics => topic_list } |
| 111 | + end |
| 112 | + navigation |
| 113 | + end |
| 114 | +end |
| 115 | + |
| 116 | +task :build do |
| 117 | + # Copy stylesheets into preview area |
| 118 | + system("cp -r #{builder_dir}/stylesheets #{preview_dir}") |
| 119 | + # Build the topic files |
| 120 | + build_config.each do |topic_group| |
| 121 | + src_group_path = File.join(source_dir,topic_group['Dir']) |
| 122 | + tgt_group_path = File.join(preview_dir,topic_group['Dir']) |
| 123 | + if not File.exists?(tgt_group_path) |
| 124 | + Dir.mkdir(tgt_group_path) |
| 125 | + end |
| 126 | + if File.exists?(File.join(src_group_path,'images')) |
| 127 | + system("cp -r #{src_group_path}/images #{tgt_group_path}") |
| 128 | + end |
| 129 | + topic_group['Topics'].each do |topic| |
| 130 | + src_file_path = File.join(src_group_path,"#{topic['File']}.adoc") |
| 131 | + tgt_file_path = File.join(tgt_group_path,"#{topic['File']}.adoc") |
| 132 | + system('cp', src_file_path, tgt_file_path) |
| 133 | + Asciidoctor.render_file tgt_file_path, :in_place => true, :safe => :unsafe, :template_dir => builder_template_dir, :attributes => ['source-highlighter=coderay','coderay-css=style',"stylesdir=#{preview_dir}/stylesheets","imagesdir=./images",'stylesheet=origin.css','linkcss!','icons=font','idprefix=','idseparator=-','sectanchors'] |
| 134 | + system('rm', tgt_file_path) |
| 135 | + end |
| 136 | + end |
| 137 | +end |
| 138 | + |
| 139 | +task :package do |
| 140 | + builds = [ |
| 141 | + { :branch => 'master', |
| 142 | + :branch_dir => ['/','openshift_origin/nightly'], |
| 143 | + :doc_version => 'OpenShift Origin Latest' |
| 144 | + }, |
| 145 | + ] |
| 146 | + |
| 147 | + branches = `git branch`.split(/\n/).map{ |branch| branch.strip } |
| 148 | + |
| 149 | + # Remeber the working branch so that we can return here later. |
| 150 | + working_branch = 'master' |
| 151 | + branches.each do |branch| |
| 152 | + next if not branch.start_with?('*') |
| 153 | + working_branch = branch.sub!(/^\* /,'') |
| 154 | + end |
| 155 | + |
| 156 | + # Make sure the working branch doesn't have any uncommitted changes |
| 157 | + changed_files = `git status --porcelain` |
| 158 | + if not changed_files.empty? |
| 159 | + puts "Warning: Your current branch has uncommitted changes. Hit <CTRL+C> if you want to exit." |
| 160 | + print "Starting packager in " |
| 161 | + [3,2,1].each do |i| |
| 162 | + print "#{i}..." |
| 163 | + sleep 1 |
| 164 | + end |
| 165 | + print "\n" |
| 166 | + end |
| 167 | + |
| 168 | + # Now make sure the build branches are available. |
| 169 | + missing_branches = false |
| 170 | + builds.each do |build| |
| 171 | + if not branches.include?(build[:branch]) |
| 172 | + if not missing_branches |
| 173 | + puts "ERROR: One or more branches for packaging are not available in this local repo:" |
| 174 | + missing_branches = true |
| 175 | + end |
| 176 | + puts "- #{build[:branch]}" |
| 177 | + end |
| 178 | + end |
| 179 | + if missing_branches |
| 180 | + puts "Add these branches and rerun the packaging command." |
| 181 | + exit 1 |
| 182 | + end |
| 183 | + |
| 184 | + # Start packaging. Clear out the old package dir before making the new package |
| 185 | + if Dir.entries('..').include?('_package') |
| 186 | + system 'rm', '-rf', '../_package' |
| 187 | + end |
| 188 | + Dir.mkdir('../_package') |
| 189 | + |
| 190 | + # Now make each package. |
| 191 | + builds.each do |build| |
| 192 | + # Check out this build branch |
| 193 | + system("git checkout #{build[:branch]}") |
| 194 | + |
| 195 | + if not $?.exitstatus == 0 |
| 196 | + puts "ERROR: Could not check out branch #{build[:branch]}, please investigate." |
| 197 | + exit 1 |
| 198 | + end |
| 199 | + |
| 200 | + # Make the build dir |
| 201 | + build_dir = "../_package/#{build[:site_dir]}" |
| 202 | + Dir.mkdir(build_dir) |
| 203 | + |
| 204 | + # Build the docs |
| 205 | + Rake::Task['build'].execute |
| 206 | + |
| 207 | + # Clean everything up |
| 208 | + Rake::Task['clean'].execute |
| 209 | + end |
| 210 | + |
| 211 | + # Return to the original branch |
| 212 | + system("git checkout #{working_branch}") |
| 213 | + |
| 214 | + puts "Packaging completed." |
| 215 | +end |
| 216 | + |
| 217 | +task :clean do |
| 218 | + sh "rm -rf ../_preview ../_package" do |ok,res| |
| 219 | + if ! ok |
| 220 | + puts "Nothing to clean." |
| 221 | + end |
| 222 | + end |
| 223 | +end |
0 commit comments