diggit 1.0.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/diggit_core.rb DELETED
@@ -1,343 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: utf-8
3
-
4
- require 'rugged'
5
- require 'oj'
6
-
7
- module Diggit
8
-
9
- DIGGIT_RC = '.dgitrc'
10
- DIGGIT_LOG = '.dgitlog'
11
- DIGGIT_SOURCES = '.dgitsources'
12
-
13
- SOURCES_FOLDER = 'sources'
14
- INCLUDES_FOLDER = 'includes'
15
- DIGGIT_FOLDER = ".diggit"
16
-
17
- # Base class for Diggit addons.
18
- # @abstract Subclass and override name to implement custom Diggit addons.
19
- #
20
- # @!attribute [r] options
21
- # @return [Hash] the Diggit options.
22
- class Addon
23
-
24
- # Create a new addon.
25
- #
26
- # @param options [Hash] a hash containing the Diggit options.
27
- def initialize(options)
28
- @options = options
29
- end
30
-
31
- # Returns the name of the addon.
32
- # @abstract The name must be overrided.
33
- #
34
- # @return [Symbol] the name of the addon.
35
- def name
36
- raise NoMethodError.new "Subclass responsability"
37
- end
38
-
39
- end
40
-
41
- # Base class for Diggit analyses. Diggit analyses are applied on each source that has been succesfully cloned. They can access the Diggit addons through the addons attribute.
42
- # @see Addon
43
- # @abstract Subclass and override run and clean to implement
44
- # a custom analysis class.
45
- # @!attribute [r] source
46
- # @return [String] the URL of the source to be analyzed.
47
- # @!attribute [r] repo
48
- # @return [Rugged::Repository] the Rugged Repository object corresponding to the source.
49
- # @!attribute [r] options
50
- # @return [Hash] a hash containing the Diggit options.
51
- # @!attribute [r] addons
52
- # @return [Hash{Symbol => Addon}] a hash containing the loaded Diggit addons, indexed by names.
53
- # @!attribute globs
54
- # @return [Hash] a hash shared between all analyses of a source.
55
- class Analysis
56
-
57
- # Create a new analysis.
58
- #
59
- # @param source [String] the URL of the source to be analyzed.
60
- # @param repo [Rugged::Repository] the Rugged Repository object corresponding to the source.
61
- # @param options [Hash] a hash containing the Diggit options.
62
- # @param addons [Hash{Symbol => Addon}] a hash containing the loaded Diggit addons, indexed by names.
63
- # @param globs [Hash] a hash shared between all analyses of a source.
64
- def initialize(source, repo, options, addons, globs)
65
- @source = source
66
- @repo = repo
67
- @options = options
68
- @addons = addons
69
- @globs = globs
70
- end
71
-
72
- # Run the analysis.
73
- # @abstract
74
- def run
75
- raise NoMethodError.new "Subclass responsability"
76
- end
77
-
78
- # Clean the data produced by the analysis.
79
- # @abstract
80
- def clean
81
- raise NoMethodError.new "Subclass responsability"
82
- end
83
-
84
- end
85
-
86
- # Base class for Diggit joins. Joins are applied after each source have been analyzed. They can access the Diggit addons through the addons attribute.
87
- # @see Addon
88
- # @abstract Subclass and override cleand and run to implement custom Diggit join.
89
- # @!attribute [r] sources
90
- # @return [Array] an array containing the finished sources.
91
- # @!attribute [r] options
92
- # @return [Hash] a hash containing the Diggit options.
93
- # @!attribute [r] addons
94
- # @return [Hash{Symbol => Addon}] a hash containing the loaded Diggit addons, indexed by names.
95
- # @!attribute globs
96
- # @return [Hash] a hash shared between all analyses of a source.
97
- class Join
98
-
99
- # Create a new join.
100
- #
101
- # @param sources [Array] an array containing the finished sources.
102
- # @param options [Hash] a hash containing the Diggit options.
103
- # @param addons [Hash{Symbol => Addon}] a hash containing the loaded Diggit addons, indexed by names.
104
- # @param globs [Hash] a hash shared between all analyses of a source.
105
- def initialize(sources, options, addons, globs)
106
- @sources = sources
107
- @options = options
108
- @addons = addons
109
- @globs = globs
110
- end
111
-
112
- # Run the join
113
- # @abstract
114
- def run
115
- raise NoMethodError.new "Subclass responsability"
116
- end
117
-
118
- # Clean the data produced by the join
119
- # @abstract
120
- def clean
121
- raise NoMethodError.new "Subclass responsability"
122
- end
123
-
124
- end
125
-
126
- class Config
127
-
128
- def initialize
129
- @config = Oj.load_file(DIGGIT_RC)
130
- end
131
-
132
- def save
133
- Oj.to_file(DIGGIT_RC, @config)
134
- end
135
-
136
- def analyses
137
- @config[:analyses]
138
- end
139
-
140
- def add_analysis(analysis)
141
- analyses << analysis unless analyses.include?(analysis)
142
- save
143
- end
144
-
145
- def rem_analysis(analysis)
146
- analyses.delete(analysis)
147
- save
148
- end
149
-
150
- def load_analyses(source, repo, addons, globs)
151
- analyses.map{ |a| Object::const_get(a).new(source, repo, options, addons, globs) }
152
- end
153
-
154
- def addons
155
- return @config[:addons]
156
- end
157
-
158
- def add_addon(addon)
159
- addons << addon unless addons.include?(addon)
160
- save
161
- end
162
-
163
- def rem_addon(addon)
164
- addons.delete(addon)
165
- save
166
- end
167
-
168
- def load_addons
169
- result = {}
170
- addons.each do |a|
171
- obj = Object::const_get(a).new(options)
172
- result[obj.name] = obj
173
- end
174
- return result
175
- end
176
-
177
- def joins
178
- return @config[:joins]
179
- end
180
-
181
- def add_join(join)
182
- joins << join unless joins.include?(join)
183
- save
184
- end
185
-
186
- def rem_join(join)
187
- joins.delete(join)
188
- save
189
- end
190
-
191
- def load_joins(finished_sources, addons, globs)
192
- return joins.map{ |j| Object::const_get(j).new(finished_sources, options, addons, globs) }
193
- end
194
-
195
- def options
196
- return @config[:options]
197
- end
198
-
199
- end
200
-
201
- class Sources
202
-
203
- def initialize
204
- @log = Log.new
205
- @sources = []
206
- IO.readlines(DIGGIT_SOURCES).each{ |line| @sources << line.strip }
207
- end
208
-
209
- def size
210
- return @sources.size
211
- end
212
-
213
- def save
214
- File.open(DIGGIT_SOURCES, "w") do |f|
215
- @sources.each{ |s| f.puts(s) }
216
- end
217
- end
218
-
219
- def add(url)
220
- unless @sources.include?(url)
221
- @sources << url
222
- @log.init(url)
223
- save
224
- end
225
- end
226
-
227
- def rem(source_def)
228
- url = url(source_def)
229
- @sources.delete(url)
230
- @log.rem(url)
231
- save
232
- end
233
-
234
- def get(source_def)
235
- hash(url(source_def))
236
- end
237
-
238
- def get_all(source_defs, filter={})
239
- sources = []
240
- if source_defs.nil? || source_defs.empty?
241
- sources = hashes
242
- else
243
- sources = source_defs.map{ |d| get(d) }
244
- end
245
- sources = sources.select{ |s| s[:log][:state] == filter[:state] } if (filter.has_key?(:state))
246
- sources = sources.select{ |s| s[:log][:error].empty? != filter[:error] } if (filter.has_key?(:error))
247
- return sources
248
- end
249
-
250
- def update(source_hash)
251
- @log.update(source_hash)
252
- end
253
-
254
- def url(source_def)
255
- url = source_def
256
- if /^\d+$/.match(source_def)
257
- idx = source_def.to_i - 1
258
- raise "Wrong source identifier: #{source_def}" if idx < 0 || idx >= @sources.size
259
- url = @sources[source_def.to_i - 1]
260
- end
261
- url
262
- end
263
-
264
- def hashes
265
- @sources.map{ |s| hash(s) }
266
- end
267
-
268
- def hash(url)
269
- {url: url, id: id(url), folder: folder(url), log: @log.log(url)}
270
- end
271
-
272
- def id(url)
273
- url.gsub(/[^[\w-]]+/, "_")
274
- end
275
-
276
- def folder(url)
277
- File.expand_path(id(url), SOURCES_FOLDER)
278
- end
279
-
280
- end
281
-
282
- class Log
283
-
284
- def initialize
285
- @log = Oj.load_file(DIGGIT_LOG)
286
- end
287
-
288
- def save
289
- Oj.to_file(DIGGIT_LOG, @log)
290
- end
291
-
292
- def init(url)
293
- unless @log.has_key?(url)
294
- @log[url] = default_log
295
- save
296
- end
297
- end
298
-
299
- def update(hash)
300
- @log[hash[:url]] = hash[:log]
301
- save
302
- end
303
-
304
- def rem(url)
305
- @log.delete(url)
306
- save
307
- end
308
-
309
- def log(url)
310
- return @log[url]
311
- end
312
-
313
- def default_log
314
- return {state: :new, error: [], analyses: []}
315
- end
316
-
317
- end
318
-
319
- class Diggit
320
- attr_accessor :sources, :config, :root
321
-
322
- def initialize(*args)
323
- super
324
- @root = FileUtils.pwd
325
- @sources = Sources.new
326
- @config = Config.new
327
- load_plugins
328
- end
329
-
330
- def load_plugins
331
- global = File.expand_path(INCLUDES_FOLDER,File.expand_path('..',File.dirname(File.realpath(__FILE__))))
332
- Dir["#{global}/**/*.rb"].each{ |f| require f }
333
-
334
- home = File.expand_path(INCLUDES_FOLDER,File.expand_path(DIGGIT_FOLDER,Dir.home))
335
- Dir["#{home}/**{,/*/**}/*.rb"].each{ |f| require f }
336
-
337
- local = File.expand_path(INCLUDES_FOLDER)
338
- Dir["#{local}/**{,/*/**}/*.rb"].each{ |f| require f }
339
- end
340
-
341
- end
342
-
343
- end
data/spec/diggit_spec.rb DELETED
@@ -1,135 +0,0 @@
1
- require 'spec_helper'
2
- require 'fileutils'
3
- require 'oj'
4
-
5
- FileUtils.rm_rf('tmp')
6
- FileUtils.mkdir('tmp')
7
- FileUtils.cd('tmp')
8
-
9
- TEST_URL = "https://github.com/jrfaller/test-git"
10
- TEST_FOLDER = "https_github_com_jrfaller_test-git"
11
- WRONG_URL = "foo"
12
-
13
- RSpec.describe Diggit::Cli::DiggitCli do
14
-
15
- it "should init a diggit folder" do
16
- result = capture(:stdout) { Diggit::Cli::DiggitCli.start(["init"]) }
17
- expect(result).to include("folder successfully initialized")
18
- expect(sources).to be_empty
19
- expect(log).to be_empty
20
- expect(config).to eq({analyses: [], addons: [], joins: [], options: {}})
21
- end
22
-
23
- it "should add an url" do
24
- Diggit::Cli::DiggitCli.start(["sources", "add", TEST_URL])
25
- expect(sources).to include(TEST_URL)
26
- end
27
-
28
- it "should add another url" do
29
- Diggit::Cli::DiggitCli.start(["sources", "add", WRONG_URL])
30
- expect(sources).to include(WRONG_URL)
31
- end
32
-
33
- it "should list the sources" do
34
- result = capture(:stdout) { Diggit::Cli::DiggitCli.start(["sources", "list"]) }
35
- expect(result).to include(TEST_URL)
36
- expect(result).to include(WRONG_URL)
37
- end
38
-
39
- it "should display the status" do
40
- result = capture(:stdout) { Diggit::Cli::DiggitCli.start(["status"]) }
41
- expect(result).to include("2 new (0 errors)")
42
- end
43
-
44
- it "should add an analysis" do
45
- Diggit::Cli::DiggitCli.start(["analyses", "add", "TestAnalysis"])
46
- expect(config[:analyses]).to eq(["TestAnalysis"])
47
- end
48
-
49
- it "should add an addon" do
50
- Diggit::Cli::DiggitCli.start(["addons", "add", "TestAddon"])
51
- expect(config[:addons]).to eq(["TestAddon"])
52
- end
53
-
54
- it "should add a join" do
55
- Diggit::Cli::DiggitCli.start(["joins", "add", "TestJoin"])
56
- expect(config[:joins]).to eq(["TestJoin"])
57
- end
58
-
59
- it "should perform clones on all urls, handling errors" do
60
- results = capture(:stdout) { Diggit::Cli::DiggitCli.start(["perform", "clones"]) }
61
- expect(results).to include("#{TEST_URL} cloned")
62
- expect(log[TEST_URL]).to include(state: :cloned, error: {})
63
- expect(File.exist?(File.expand_path(TEST_FOLDER,Diggit::SOURCES_FOLDER))).to be true
64
- expect(results).to include("error cloning foo")
65
- expect(log[WRONG_URL][:state]).to eq(:new)
66
- expect(log[WRONG_URL][:error]).to include(name: "Rugged::NetworkError")
67
- end
68
-
69
- it "should display info on a regular url" do
70
- results = capture(:stdout) { Diggit::Cli::DiggitCli.start(["sources", "info", TEST_URL]) }
71
- expect(results).to include("cloned")
72
- expect(results).to include(TEST_URL)
73
- end
74
-
75
- it "should display info and error on an url in error" do
76
- results = capture(:stdout) { Diggit::Cli::DiggitCli.start(["sources", "info", WRONG_URL]) }
77
- expect(results).to include("new")
78
- expect(results).to include(WRONG_URL)
79
- expect(results).to include("error")
80
- expect(results).to include("Rugged::NetworkError")
81
- end
82
-
83
- it "should display all errors" do
84
- results = capture(:stdout) { Diggit::Cli::DiggitCli.start(["sources", "errors"]) }
85
- expect(results).to include("new")
86
- expect(results).to include(WRONG_URL)
87
- expect(results).to include("error")
88
- expect(results).to include("Rugged::NetworkError")
89
- end
90
-
91
- it "should remove urls" do
92
- Diggit::Cli::DiggitCli.start(["sources", "rem", WRONG_URL])
93
- expect(sources).to_not include(WRONG_URL)
94
- end
95
-
96
- it "should perform analyses" do
97
- results = capture(:stdout) { Diggit::Cli::DiggitCli.start(["perform", "analyses"]) }
98
- expect(results).to include("TestAnalysis performed")
99
- expect(results).to include("source #{TEST_URL} analyzed")
100
- expect(log[TEST_URL][:state]).to eq(:finished)
101
- end
102
-
103
- it "should perform joins" do
104
- results = capture(:stdout) { Diggit::Cli::DiggitCli.start(["perform", "joins"]) }
105
- expect(results).to include("TestJoin performed")
106
- expect(results).to include("joins performed")
107
- end
108
-
109
- it "should clean joins" do
110
- results = capture(:stdout) { Diggit::Cli::DiggitCli.start(["clean", "joins"]) }
111
- expect(results).to include("TestJoin cleaned")
112
- end
113
-
114
- it "should clean analyses" do
115
- results = capture(:stdout) { Diggit::Cli::DiggitCli.start(["clean", "analyses"]) }
116
- expect(results).to include("TestAnalysis cleaned on #{TEST_URL}")
117
- expect(log[TEST_URL][:state]).to eq(:cloned)
118
- end
119
-
120
- it "should remove a join" do
121
- Diggit::Cli::DiggitCli.start(["joins", "rem", "TestJoin"])
122
- expect(config[:addons]).not_to include("TestJoin")
123
- end
124
-
125
- it "should remove an analysis" do
126
- Diggit::Cli::DiggitCli.start(["analyses", "rem", "TestAnalysis"])
127
- expect(config[:analyses]).not_to include("TestAnalysis")
128
- end
129
-
130
- it "should remove an addon" do
131
- Diggit::Cli::DiggitCli.start(["addons", "rem", "TestAddon"])
132
- expect(config[:addons]).not_to include("TestAddon")
133
- end
134
-
135
- end