Class: KCommercial::Resources::GitSource

Inherits:
Object
  • Object
show all
Extended by:
Pod::Executable
Includes:
SourceInterface
Defined in:
lib/KCommercialPipeline/core/resource/source/git_source.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RubyLanguage::Interface

#interface

Constructor Details

#initialize(url, root_path) ⇒ GitSource

Returns a new instance of GitSource.

Parameters:



23
24
25
26
# File 'lib/KCommercialPipeline/core/resource/source/git_source.rb', line 23

def initialize(url, root_path)
  @url = url
  @root_path = root_path
end

Instance Attribute Details

#root_pathPathname (readonly)

The local storage path

Returns:



19
20
21
# File 'lib/KCommercialPipeline/core/resource/source/git_source.rb', line 19

def root_path
  @root_path
end

#urlString (readonly)

The url for the git repo

Returns:



15
16
17
# File 'lib/KCommercialPipeline/core/resource/source/git_source.rb', line 15

def url
  @url
end

Instance Method Details

#all_assets(type) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/KCommercialPipeline/core/resource/source/git_source.rb', line 28

def all_assets(type)
  directory_name = type_directory_mapper[type]
  return [] unless directory_name
  asset_paths = root_path.glob("**/#{directory_name}")
  return [] unless asset_paths
  total_assets = []
  asset_paths.each do |path|
    assets = []
    if [ KCommercial::Resources::AssetType::Font].include? type
      assets = assets_from_directory path, HashAsset
    elsif [KCommercial::Resources::AssetType::Color, KCommercial::Resources::AssetType::I18n].include? type
      assets = assets_from_directory path, StringAsset
    elsif [KCommercial::Resources::AssetType::Image].include? type
      assets = assets_from_directory path, ArrayHashAsset
    else
      assets = assets_from_directory path, Asset
    end
    assets.each { |asset|
      total_assets.append(asset)
    }
  end
  total_assets
end

#assets_from_directory(directory, asset_class) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/KCommercialPipeline/core/resource/source/git_source.rb', line 52

def assets_from_directory(directory, asset_class)
  asset_paths = directory.glob('**/*.cdasset')
  asset_paths.map do |path|
    name = path.basename('.*').to_s
    asset_class.new(name, path)
  end
end

#cloneObject



92
93
94
# File 'lib/KCommercialPipeline/core/resource/source/git_source.rb', line 92

def clone
  git! ['clone', url, @root_path.expand_path]
end

#git_updateObject



96
97
98
99
100
101
# File 'lib/KCommercialPipeline/core/resource/source/git_source.rb', line 96

def git_update
  KCommercial.UI.info "Updating the source #{url}"
  Dir.chdir(@root_path) do
    git! ['pull', '--rebase']
  end
end

#struct_assets_from_directory(directory) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/KCommercialPipeline/core/resource/source/git_source.rb', line 60

def struct_assets_from_directory(directory)
  asset_paths = directory.glob('**/*.cdasset')
  asset_paths.map do |path|
    name = path.basename('.*').to_s
    HashAsset.new(name, path)
  end
end

#type_directory_mapperObject



70
71
72
73
74
75
76
77
78
# File 'lib/KCommercialPipeline/core/resource/source/git_source.rb', line 70

def type_directory_mapper
  {
    AssetType::Color => 'Colors',
    AssetType::File => 'Files',
    AssetType::Font => 'Fonts',
    AssetType::I18n => 'I18ns',
    AssetType::Image => 'Images',
  }
end

#updateObject

Update the source



81
82
83
84
85
86
87
88
89
90
# File 'lib/KCommercialPipeline/core/resource/source/git_source.rb', line 81

def update
  if not @root_path.exist?
    clone
  elsif not @root_path.join('.git').exist?
    @root_path.rmtree
    clone
  else
    git_update
  end
end