Class: Richcss::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/richcss/manager.rb

Class Method Summary collapse

Class Method Details

.check(check_dir) ⇒ Object

Checks if the folder has the required format for uploading



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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
# File 'lib/richcss/manager.rb', line 10

def self.check(check_dir)
  Dir.chdir(check_dir)

  # LEVEL 1
  # Find the spec file and the part name
  part_name = ''
  specFilePath = ''
  Dir.glob("*.spec").each do |f|
    part_name = File.basename(f, '.spec')
    specFilePath = "#{check_dir}/#{f}"
  end
  if part_name.empty?
    return "Rich CSS spec file not found"
  end

  # Check if readme exists
  if !File.file?("#{check_dir}/README.md")
    return "README.md file not found"
  end

  if !Dir.exist?('lib')
    return "lib folder not found"
  end 

  # LEVEL 2 / LIB
  Dir.chdir('lib')
  groups = ['box', 'elements']

  groups.each do |g|
    if !Dir.exist?(g)
      return "#{g} folder not found in lib"
    end
  end

  # LEVEL 3 BOX/ELEMENTS/PARTS
  # TODO: Don't do this hardcoded for box/elements, use like some hashmap or something..
  boxFiles = ['blocks', 'main', 'positioning'] 
  elementFiles = ['button', 'colors', 'features', 'fonts', 'images', 'inputs', 'lists']
  validExtensions = ['.css', '.css.scss']

  fileCount = 0
  Dir.foreach('box') do |filename|
    next if filename == '.' or filename == '..'
    boxFiles.each do |b|
      validExtensions.each do |ext|
        if filename == "#{b}#{ext}"
          fileCount += 1
        end
      end
    end
  end

  if fileCount < boxFiles.size
    return "Missing css files in box folder, required #{boxFiles}"
  end

  fileCount = 0
  Dir.foreach('elements') do |filename|
    next if filename == '.' or filename == '..'
    elementFiles.each do |b|
      validExtensions.each do |ext|
        if filename == "#{b}#{ext}"
            fileCount += 1
        end
      end
    end
  end

  if fileCount < elementFiles.size
    return "Missing css files in elements folder, required #{elementFiles}"
  end

  # SPEC FILE CHECK
  specFile = "#{part_name}.spec"

  begin
    jsonData = File.read(specFilePath) 
    hash = JSON.parse(jsonData)
  rescue
    return "Invalid Json format in #{specFile}"  
  end
  
  defaultSpecs = {
    "authors" => "AUTHOR_NAME",
    "email" => "AUTHOR_EMAIL",
    "description" => "DESCRIPTION",
    "homepage" => "GITHUB_REPO_URL"
  }
  requiredSpecs = ['name', 'authors', 'email', 'description', 'version', 'homepage', 'dependencies']

  # Ensure each spec exist
  requiredSpecs.each do |spec|
    if hash[spec].nil? && spec != 'dependencies'
      return "Missing \"#{spec}\" definition in #{specFile}"
    end
  end

  # Check for default entries
  defaultSpecs.keys.each do |spec|
    if hash[spec] == defaultSpecs[spec]
      return "Default value for \"#{spec}\" in #{specFile} is being used, please change it to a valid entry"
    end
  end

  # Check email
  if !EmailValidator.valid?(hash[requiredSpecs[2]])
    return "Email address is invalid"
  end

  # Check Part_Name
  if (hash[requiredSpecs[0]] != part_name)
    return "Invalid part name: \"#{hash[requiredSpecs[0]]}\" in #{specFile}, should be \"#{part_name}\""
  end


  # Check if github url exist
  uri = URI.parse(hash[requiredSpecs[5]])

  http_object = Net::HTTP.new(uri.host, uri.port)
  http_object.use_ssl = true if uri.scheme == 'https'
  begin
    http_object.start do |http|
      request = Net::HTTP::Get.new uri.request_uri
      http.read_timeout = 500
      http.request request do |response|
        case response
        when Net::HTTPNotFound then
          return "Could not access GitHub url, please use a public repository"
        end
      end
    end
  rescue
    return "Invalid URL for homepage"
  end

  # Check for version
  begin
   resp = RestClient.get "http://www.cssparts.com/api/part/#{part_name}"
    if resp.code == 200
    	body = JSON.parse(resp.to_str)
    	current_version = body["version"]
    	part_version = hash[requiredSpecs[4]]
      if Gem::Version.new(current_version) >= Gem::Version.new(part_version)
      	return "Part version: \"#{part_version}\" in #{specFile} must be greater than existing version: \"#{current_version}\""
      end
    end
 rescue RestClient::ExceptionWithResponse => e
  end

  # Check dependency existance
  dependencies = hash[requiredSpecs[6]]
  if dependencies.nil? || dependencies.empty?
    return nil
  end

  begin
  	dependencies = dependencies.to_a.map { |x| "#{x[0]}=#{x[1].to_s}" }.join("&")
    resp = RestClient.get "http://www.cssparts.com/api/validateDependencies/#{dependencies}"
  rescue RestClient::ExceptionWithResponse => e
    return e.response
  end 

  return nil
end

.getPartData(part_name) ⇒ Object

Fetch url and download the part



176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/richcss/manager.rb', line 176

def self.getPartData(part_name)
  begin
    resp = RestClient.get "http://www.cssparts.com/api/part/#{part_name}"
    if resp.code == 200
      body = JSON.parse(resp.to_str)
      return body
    else
      puts "Error: Part #{name} cannot be found."
    end
  rescue RestClient::ExceptionWithResponse => e
    puts e.response
  end 
  return nil
end

.upload(part_path) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/richcss/manager.rb', line 191

def self.upload(part_path)
  partPathSplit = part_path.split("/")
  partName = partPathSplit[partPathSplit.length - 1]

  if !File.file?("#{part_path}/#{partName}.spec")
    puts "#{part_path}/#{partName}.spec file not found"
    return
  end

  specs = File.read("#{part_path}/#{partName}.spec")
  specsJson = JSON.parse(specs)

  begin
 puts RestClient.post "http://www.cssparts.com/api/upload", :name => partName, :description => specsJson["description"],
   :version => specsJson["version"], :authors => specsJson["authors"], :email => specsJson["email"], :homepage => specsJson["homepage"],
   :dependencies => specsJson["dependencies"]
	  rescue RestClient::ExceptionWithResponse => e
    puts e.response
  end 
end