Class: HeyDan::Sources

Inherits:
Object
  • Object
show all
Defined in:
lib/heydan/sources.rb

Class Method Summary collapse

Class Method Details

.add(link) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/heydan/sources.rb', line 18

def add(link)
  raise 'Link must be a git link in the format of http(s)://url/*.git' if !correct_link?(link)
  settings_file = HeyDan::Base.load_or_create_settings
  name = extract_name(link)
  if !source_exists?(link)
    HeyDan.sources ||= {} 
    HeyDan.sources.merge!({"#{name}" => link})
    HeyDan::Base.save_settings
  end
  update(name)
end

.build(folder = nil, name = nil, variable = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/heydan/sources.rb', line 92

def build(folder=nil, name=nil, variable=nil)
  if variable && name && folder
    build_variable(folder, name, variable)
  elsif name && folder
    build_source(folder, name)
  elsif folder
    build_folder(folder)
  else
    HeyDan.sources.keys.each { |source| build_folder(source.to_s)}
  end
end

.build_folder(folder) ⇒ Object



108
109
110
111
112
# File 'lib/heydan/sources.rb', line 108

def build_folder(folder)
  sources(folder).each do |source|
    build_source(folder, source)
  end
end

.build_source(folder, source) ⇒ Object



114
115
116
117
118
119
# File 'lib/heydan/sources.rb', line 114

def build_source(folder, source)
  source_file = HeyDan::SourceFile.new(folder, source)
  source_file.variables.each do |var|
    build_variable(folder, source, var)
  end
end

.build_variable(folder, source, variable) ⇒ Object



121
122
123
124
125
# File 'lib/heydan/sources.rb', line 121

def build_variable(folder, source, variable)
  script = HeyDan::ScriptFile.new(folder, source, variable)
  var = script.eval_class.send(:new, {folder: folder, source: source, variable: variable}.merge(HeyDan.options))
  var.process
end

.correct_link?(link) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/heydan/sources.rb', line 30

def correct_link?(link)
  !link.match(/(http|https):\/\/\w+\.\w+(\/\w+)+\.git/).nil?
end

.create(folder, source_name, variable = nil) ⇒ Object



86
87
88
89
90
# File 'lib/heydan/sources.rb', line 86

def create(folder, source_name, variable=nil)
  create_folder(folder) if !directory_exist?(folder)
  create_source(folder, source_name) if !source_exist?(folder, source_name)
  create_variable(folder, source_name, variable) if !variable_exist?(folder, source_name, variable)
end

.create_folder(name) ⇒ Object



57
58
59
# File 'lib/heydan/sources.rb', line 57

def create_folder(name)
  FileUtils.mkdir_p source_folder(name)
end

.create_source(folder, name) ⇒ Object



61
62
63
64
# File 'lib/heydan/sources.rb', line 61

def create_source(folder, name)
  file = HeyDan::SourceFile.new(folder, name)
  file.save
end

.create_variable(folder, name, variable) ⇒ Object



66
67
68
69
70
# File 'lib/heydan/sources.rb', line 66

def create_variable(folder, name, variable)
  file = HeyDan::SourceFile.new(folder, name)
  file.add_variable(variable)
  file.save
end

.directory_exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/heydan/sources.rb', line 49

def directory_exist?(name)
  Dir.exists?(source_folder(name))
end

.extract_name(git_link) ⇒ Object



53
54
55
# File 'lib/heydan/sources.rb', line 53

def extract_name(git_link)
  git_link.match(/(\w+)\.git$/i)[1]
end

.only_letters_and_underscores?(text) ⇒ Boolean

Returns:

  • (Boolean)


83
84
# File 'lib/heydan/sources.rb', line 83

def only_letters_and_underscores?(text)
end

.source_exist?(folder, source_name) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
# File 'lib/heydan/sources.rb', line 72

def source_exist?(folder, source_name)
  file = HeyDan::SourceFile.new(folder, source_name)
  file.exist?
end

.source_exists?(name_or_link) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
9
# File 'lib/heydan/sources.rb', line 6

def source_exists?(name_or_link)
  HeyDan.sources ||= {} 
  HeyDan.sources.keys.map(&:to_s).include?(name_or_link) || HeyDan.sources.values.include?(name_or_link)
end

.source_folder(name) ⇒ Object



45
46
47
# File 'lib/heydan/sources.rb', line 45

def source_folder(name)
  File.join(HeyDan.folders[:sources],name)
end

.sources(folder) ⇒ Object



104
105
106
# File 'lib/heydan/sources.rb', line 104

def sources(folder)
  Dir.glob(source_folder(folder) + '/*').select { |x| !x.include?('/scripts') && !x.include?('.')}.map { |x| x.split('/')[-1]}
end

.syncObject



11
12
13
14
15
16
# File 'lib/heydan/sources.rb', line 11

def sync
  keys = HeyDan.sources.keys
  if keys
    HeyDan.sources.keys.map(&:to_s).each { |source| update(source)}
  end
end

.update(name) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/heydan/sources.rb', line 34

def update(name)
  if directory_exist?(name)
    HeyDan::HelpText.git_update(name)
    g = Git.open(source_folder(name))
    g.pull
  else
    HeyDan::HelpText.git_clone(name)
    g = Git.clone(HeyDan.sources[name.to_sym], name, {:path => HeyDan.folders[:sources]})
  end
end

.variable_exist?(folder, source_name, variable) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
# File 'lib/heydan/sources.rb', line 77

def variable_exist?(folder, source_name, variable)
  file = HeyDan::SourceFile.new(folder, source_name)
  return file.exist? if !file.exist?
  !file.variable(variable).nil?
end