Class: Env

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

Constant Summary collapse

CONFIG_SUB_DIR =
'env'.freeze
ROOT_PATH =
File.join(Config::CONFIG_DIR, CONFIG_SUB_DIR).freeze
CONFIG_FILE =
File.join(ROOT_PATH, 'env.json').freeze
FILES_PATH =
File.join(ROOT_PATH, 'files').freeze
FOLDERS_PATH =
File.join(ROOT_PATH, 'folders').freeze
DEFAULT_CONFIG =
{
  'files' => [],
  'folders' => []
}.freeze

Instance Method Summary collapse

Instance Method Details

#add(file) ⇒ Object



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
# File 'lib/env/env.rb', line 23

def add(file)
  abs_file = File.absolute_path(file.dup)

  config = Config.get_config_json(CONFIG_FILE, DEFAULT_CONFIG)
  list = get_sync_list(config, abs_file)

  parsed_file = FileUtil.to_local_file(abs_file)

  local_path = get_local_path(file)

  unless list.include? parsed_file
    dir = File.dirname(parsed_file)

    puts File.join(local_path, dir)

    FileUtils.mkdir_p File.join(local_path, dir)

    if File.file?(abs_file)
      FileUtils.cp(abs_file, File.join(local_path, parsed_file))
    else
      FileUtils.cp_r(abs_file, File.join(local_path, parsed_file))
    end

    list.append(parsed_file) if File.exist? File.join(local_path, parsed_file)
  end

  Config.write_config_file(CONFIG_FILE, config)

  Log::LOGGER.info("Added '#{parsed_file}'")
end

#applyObject



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
# File 'lib/env/env.rb', line 73

def apply
  Log::LOGGER.info('Start apply')

  config = Config.get_config_json(CONFIG_FILE, DEFAULT_CONFIG)
  files = config['files']

  files.each do |file|
    dest_file = FileUtil.to_system_file(file)

    abs_file = File.join(FILES_PATH, file)
    Log::LOGGER.debug("Copy #{abs_file} to #{dest_file}")

    FileUtils.cp_r(abs_file, dest_file, remove_destination: true)
  end

  folders = config['folders']

  folders.each do |folder|
    dest_file = File.dirname(FileUtil.to_system_file(folder))

    abs_file = File.join(FOLDERS_PATH, folder)
    Log::LOGGER.debug("Copy #{abs_file} to #{dest_file}")

    FileUtils.cp_r(abs_file, dest_file, remove_destination: true)
  end

  Log::LOGGER.info('Finished apply')
end

#get_local_path(file) ⇒ Object



131
132
133
# File 'lib/env/env.rb', line 131

def get_local_path(file)
  return File.file?(file) ? FILES_PATH : FOLDERS_PATH
end

#get_sync_list(config, file) ⇒ Object



135
136
137
# File 'lib/env/env.rb', line 135

def get_sync_list(config, file)
  return File.file?(file) ? config['files'] : config['folders']
end

#remove(file) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/env/env.rb', line 54

def remove(file)
  local_file = FileUtil.to_local_file(file)
  local_file_abs = File.join(get_local_path(file), local_file)

  config = Config.get_config_json(CONFIG_FILE, DEFAULT_CONFIG)
  list = get_sync_list(config, local_file_abs)

  if list.include? local_file
    puts(local_file_abs)
    FileUtils.rm_rf(local_file_abs)

    list.delete(local_file)
  end

  Config.write_config_file(CONFIG_FILE, config)

  Log::LOGGER.info("Removed '#{local_file}'")
end

#syncObject



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
# File 'lib/env/env.rb', line 102

def sync
  Log::LOGGER.info('Start sync')

  config = Config.get_config_json(CONFIG_FILE, DEFAULT_CONFIG)
  files = config['files']

  files.each do |file|
    dest_file = FileUtil.to_system_file(file)

    abs_file = File.join(FILES_PATH, file)
    Log::LOGGER.debug("Copy #{dest_file} to #{abs_file}")

    FileUtils.cp_r(dest_file, abs_file, remove_destination: true)
  end

  folders = config['folders']

  folders.each do |folder|
    dest_file = FileUtil.to_system_file(folder)

    abs_file = File.dirname(File.join(FOLDERS_PATH, folder))
    Log::LOGGER.debug("Copy #{dest_file} to #{abs_file}")

    FileUtils.cp_r(dest_file, abs_file, remove_destination: true)
  end

  Log::LOGGER.info('Finished sync')
end