Class: DashcodeConverter::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/dashcode-converter/project.rb

Constant Summary collapse

PROJECT_TEMPLATE =
<<-EOF
  name: <%=name%>
  version: 1.0.0
  notice: src/NOTICE
  type: application
  export: <%=name%>
  output folder: build

  external:
    - name: coherent
      path: ext/coherent
      repository: https://github.com/jeffwatkins/coherent.git

  include:
    - coherent
  
  source:
    - src/index.html
    - src/<%=name%>.jsnib
EOF

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_bundle, output_folder) ⇒ Project

Returns a new instance of Project.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dashcode-converter/project.rb', line 31

def initialize(project_bundle, output_folder)
  @project_bundle= File.expand_path(project_bundle)
  @name= File.basename(@project_bundle, ".*")
  @namespace= @name
  @output_folder= File.expand_path(File.join(output_folder, @name))
  @nib_folder= "#{@name}.#{BUNDLE_EXTENSION}"
  @nib_output_folder= File.join(@output_folder, "src", @nib_folder)
  
  @images_output_folder= File.join(@output_folder, "src", @nib_folder, "images")
  
  @parts_spec_path= File.join(@project_bundle, "project", "safari", "Parts", "setup.js")
  @datasources_spec_path= File.join(@project_bundle, "project", "Parts", "datasources.js")
  @css_path= File.join(@project_bundle, "project", "safari", "main.css")
  @markup_path= File.join(@project_bundle, "project", "index.html")
  @scripts= Scripts.new(File.join(@project_bundle, "project", "safari"))
  
  @controller_name= "#{@name.capitalize}Controller"
  
  FileUtils.mkdir_p(@output_folder)
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/dashcode-converter/project.rb', line 8

def name
  @name
end

#namespaceObject

Returns the value of attribute namespace.



8
9
10
# File 'lib/dashcode-converter/project.rb', line 8

def namespace
  @namespace
end

Instance Method Details

#controllerObject



81
82
83
84
# File 'lib/dashcode-converter/project.rb', line 81

def controller
  return @controller if @controller
  @controller= Controller.new(@name, @namespace, @scripts)
end

#convertObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/dashcode-converter/project.rb', line 149

def convert
  fixup_html
  
  FileUtils.mkdir_p(@nib_output_folder)
  FileUtils.mkdir_p(@output_folder)
  FileUtils.mkdir_p(@images_output_folder)

  Dir.chdir(@output_folder) do
    File.open("#{@name}.jsproj", "w") { |project_file|
      project_file << ERB.new(PROJECT_TEMPLATE.remove_indent).result(binding)
    }
  end
  
  Dir.chdir(File.join(@output_folder, "src")) do
    File.open("index.html", "w") { |html|
      html <<%{
        <!DOCTYPE HTML>
        <html>
          <head>
            <link rel="stylesheet" href="#{@name}.css" type="text/css" media="screen" charset="utf-8">
            <script src="#{@name}.js" type="text/javascript" charset="utf-8"></script>
          </head>
  
          <body>
          </body>
          <script type="text/javascript" charset="utf-8">
            distil.onready(function(){

              var controller= new #{@name}.#{@controller_name}({
                                  nibName: '#{@name}'
                                });
              controller.view();
  
            });
  
          </script>
        </html>
      }.remove_indent
    }
  end
  
  Dir.chdir(@nib_output_folder) do
    File.open("#{@controller_name}.js", "w") { |controller_file|
      controller_file << controller.declaration
    }
    File.open("#{@name}.js", "w") { |nib_file|
      nib_file << nib.declaration
    }
    File.open("#{@name}.css", "w") { |css_file|
      css_file << css
    }
    File.open("#{@name}.html", "w") { |html_file|
      html_file << doc.css("body > *:first-child")[0].serialize
    }
  end
  
  Dir.chdir(@output_folder) do
    system 'distil'
  end
  
end

#cssObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/dashcode-converter/project.rb', line 95

def css
  text= File.read(@css_path)
  dirname= File.dirname(@css_path)
  text.gsub!(CSS_IMAGE_URL_REGEX) { |match|
    image_file= File.join(dirname, $1)

    if (!File.exists?(image_file))
      match
    else
      new_image_file= File.join(@images_output_folder, File.basename(image_file))
      FileUtils.mkdir_p(File.dirname(new_image_file))
      FileUtils.cp image_file, new_image_file
      "url(\"#{relative_path(new_image_file)}\")"
    end
  }
  text
end

#docObject



74
75
76
77
78
79
# File 'lib/dashcode-converter/project.rb', line 74

def doc
  return @doc if @doc
  
  html= File.read(@markup_path)
  @doc= Nokogiri::HTML(html)
end

#fixup_htmlObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/dashcode-converter/project.rb', line 113

def fixup_html
  base= doc.css("base")
  base= base ? base.attribute("href") : nil
  
  content= doc.css("body > *:first-child")[0]
  content.traverse { |node|
    next unless node.attributes
    node.attributes.each { |attr, value|
      if 0==attr.index("apple-")
        node.remove_attribute(attr)
      end
    }
  }
  
  dirname= File.dirname(@markup_path)
  dirname= File.join(dirname, base) if base
  
  content.css("[src]").each { |node|
    image_file= File.join(dirname, node.attribute('src'))
    new_image_file= File.join(@images_output_folder, File.basename(image_file))
    FileUtils.mkdir_p(File.dirname(new_image_file))
    FileUtils.cp image_file, new_image_file
    node["src"]= relative_path(new_image_file)
  }
  
  nib.views.each { |view|
    # Use a copy of the view's items, because the iterator isn't stable if
    # items are removed while iterating.
    items= view.items.clone
    items.each { |item|
      html= doc.css(item.name)[0]
      item.fixup_html(html)
    }
  }
end

#nibObject



86
87
88
89
90
91
92
93
# File 'lib/dashcode-converter/project.rb', line 86

def nib
  return @nib if @nib
  
  @nib= Nib::Nib.new(@name, controller)
  @nib.add_view_from_path(@parts_spec_path)
  @nib.add_datasources_from_path(@datasources_spec_path)
  @nib
end

#path_relative_to_folder(path, folder) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/dashcode-converter/project.rb', line 52

def path_relative_to_folder(path, folder)
  path= File.expand_path(path)
  outputFolder= File.expand_path(folder).to_s
  
  # Remove leading slash and split into parts
  file_parts= path.slice(1..-1).split('/');
  output_parts= outputFolder.slice(1..-1).split('/');

  common_prefix_length= 0

  file_parts.each_index { |i|
    common_prefix_length= i
    break if file_parts[i]!=output_parts[i]
  }

  return '../'*(output_parts.length-common_prefix_length) + file_parts[common_prefix_length..-1].join('/')
end

#relative_path(path) ⇒ Object



70
71
72
# File 'lib/dashcode-converter/project.rb', line 70

def relative_path(path)
  path_relative_to_folder(path, @nib_output_folder)
end