-
Notifications
You must be signed in to change notification settings - Fork 17
/
page_writer.rb
54 lines (45 loc) · 1.92 KB
/
page_writer.rb
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
require "plist"
require "renderer/page_processor"
module Playgroundbook
class PageWriter
def initialize(page_processor = PageProcessor.new, ui = Cork::Board.new)
@page_processor = page_processor
@ui = ui
end
def write_page(page_name, page_dir_name, imports, page_contents, page_sources, page_resources, chapter_info={})
Dir.mkdir(page_dir_name) unless Dir.exist?(page_dir_name)
contents_with_import = "//#-hidden-code\n"
contents_with_import += imports.map { |i| "import #{i}" }.join("\n") + "\n"
contents_with_import += "//#-end-hidden-code\n"
contents_with_import += @page_processor.strip_extraneous_newlines(page_contents)
Dir.chdir(page_dir_name) do
File.open(ContentsSwiftFileName, "w") do |file|
file.write(contents_with_import)
end
File.open(MANIFEST_FILE_NAME, "w") do |file|
file.write({
"Name" => page_name,
"LiveViewMode" => chapter_info.has_key?("live_view_mode") ? chapter_info["live_view_mode"] : "HiddenByDefault",
"LiveViewEdgeToEdge" => chapter_info.has_key?("edge_to_edge_live_view") ? chapter_info["edge_to_edge_live_view"] : true,
"Version" => "1.0",
"ContentVersion" => "1.0"
}.to_plist)
end
copy_page_sources(page_sources)
copy_page_resources(page_resources)
end
end
def copy_page_sources(source_names)
Dir.mkdir(SharedSourcesDirectoryName) unless Dir.exist?(SharedSourcesDirectoryName)
source_names.each do |source|
FileUtils.cp("../../../../../../#{source}", SharedSourcesDirectoryName)
end
end
def copy_page_resources(resource_names)
Dir.mkdir(SharedResourcesDirectoryName) unless Dir.exist?(SharedResourcesDirectoryName)
resource_names.each do |resource|
FileUtils.cp("../../../../../../#{resource}", SharedResourcesDirectoryName)
end
end
end
end