This repository was archived by the owner on Nov 1, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathenterprise-backport
More file actions
executable file
·126 lines (98 loc) · 3.55 KB
/
enterprise-backport
File metadata and controls
executable file
·126 lines (98 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env ruby
require 'bundler/setup'
require 'fileutils'
require 'nanoc'
require 'yaml'
require 'json'
require 'active_support/core_ext/hash'
Dir.glob('tasks/*.rake').each { |r| load r }
start_message
#################################################################
## Phase 1: Build the versioned content ##
#################################################################
def build_the_versioned_content
config = YAML.load_file('nanoc.yaml')
# set page.version for every article to <CURR_VERSION>
defaults = config['page_variables']
defaults.first['values']['version'] = CURR_VERSION
yaml = """
is_enterprise_help: true
audience: #{CURR_VERSION}
version: #{CURR_VERSION}
latest_enterprise_version: &latest_enterprise_version #{CURR_VERSION}
data_variables:
-
scope:
path: ''
values:
version: #{CURR_VERSION}
"""
config_enterprise_admin = Tempfile.new('_config.enterprise.yml')
config_enterprise_admin.write(yaml.strip)
config_enterprise_admin.rewind
config_enterprise_admin.close
config_enterprise_admin = YAML.load_file(config_enterprise_admin.path)
versioned_config = symbolize_hash(config.deep_merge(config_enterprise_admin))
ent_scope_idx = versioned_config[:page_variables].find_index { |v| v[:scope][:path] == 'enterprise/.' }
versioned_config[:page_variables][ent_scope_idx][:values][:version] = CURR_VERSION
Nanoc::Int::SiteLoader.new.new_with_config(versioned_config).compile
puts `node_modules/gulp/bin/gulp.js assets`
fail unless $CHILD_STATUS.to_i == 0
versioned_config
end
#################################################################
## Phase 2: Rewrite the generated content ##
#################################################################
def rewrite_generated_content
Dir.glob('output/**/*.html') do |f|
doc = Nokogiri::HTML.parse(File.read(f))
doc = rewrite_asset_paths(doc)
doc = rewrite_anchors(doc)
html = doc.to_html.to_s
html = rewrite_whitespace(html)
File.write(f, html)
end
Dir.glob('output/**/*.css') do |css_path|
rewrite_octicons_path(css_path)
end
search_json = File.read('output/search/search-index.json')
search_json = rewrite_whitespace(search_json)
File.write('output/search/search-index.json', search_json)
end
#################################################################
## Phase 3: Remove non-existent content ##
#################################################################
def remove_nonexistent_content
# find all the valid files based on the sidebar links
v3_api_sidebar = File.open("#{VERSIONED_ENT_PATH}/v3/index.html") { |f| Nokogiri::XML(f) }
pages = []
v3_api_sidebar.css('#js-sidebar a').each do |a|
href = a['href']
next if href == '#' || !href.start_with?("/#{VERSIONED_ENT_PATH}/v3")
pages << href
end
# remove non-existent files from search index
search_json_path = "#{VERSIONED_ENT_PATH}/search/search-index.json"
search_json = JSON.parse(File.read(search_json_path))
kept_json = []
removed_files = []
search_json.each do |e|
url = e['url']
next if url.nil?
if pages.include?(url) || url !~ %r{^/#{VERSIONED_ENT_PATH}/v3/}
kept_json << e
else
removed_files << url[1..-1] # trim starting "/"
end
end
File.write(search_json_path, JSON.pretty_generate(kept_json))
# remove non-existent files from disk
removed_files.each { |f| FileUtils.rm_rf(f) }
end
prepare
build_the_versioned_content
rewrite_generated_content
copy_to_destination
remove_nonexistent_content
cleanup
puts 'All done!'