Class: Util

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/util.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUtil

Returns a new instance of Util.



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

def initialize
    #本地主题的目录
    @local_theme_dir = 'm2m-theme'
    #当前的工作目录
    @workbench = nil
    
    #配置文件的文件名
    @config_file = 'm2m.config'
    @encrypt_key = 'm2m'
end

Instance Attribute Details

#config_fileObject (readonly)

Returns the value of attribute config_file.



12
13
14
# File 'lib/util.rb', line 12

def config_file
  @config_file
end

#encrypt_keyObject (readonly)

Returns the value of attribute encrypt_key.



16
17
18
# File 'lib/util.rb', line 16

def encrypt_key
  @encrypt_key
end

#local_theme_dirObject (readonly)

Returns the value of attribute local_theme_dir.



13
14
15
# File 'lib/util.rb', line 13

def local_theme_dir
  @local_theme_dir
end

#project_nameObject (readonly)

Returns the value of attribute project_name.



14
15
16
# File 'lib/util.rb', line 14

def project_name
  @project_name
end

#workbenchObject

Returns the value of attribute workbench.



15
16
17
# File 'lib/util.rb', line 15

def workbench
  @workbench
end

Instance Method Details

#decrypt(str, encrypt_key = @encrypt_key) ⇒ Object



116
117
118
# File 'lib/util.rb', line 116

def decrypt(str, encrypt_key = @encrypt_key)
    AESCrypt.decrypt(str, encrypt_key)
end

#encrypt(str, encrypt_key = @encrypt_key) ⇒ Object

加解密 ####################



112
113
114
# File 'lib/util.rb', line 112

def encrypt(str, encrypt_key = @encrypt_key)
    AESCrypt.encrypt(str, encrypt_key)
end

#error(log, level = 1) ⇒ Object



90
91
92
93
# File 'lib/util.rb', line 90

def error(log, level = 1)
    puts log
    exit level
end

#get_merge_path(relative_path, base_path = Dir::pwd) ⇒ Object

合并两个路径



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

def get_merge_path(relative_path, base_path = Dir::pwd)
    base_path = Pathname.new base_path
    return base_path if not relative_path
    base_path + Pathname.new(relative_path)
end

#get_productObject

获取产品相关的信息



32
33
34
35
36
37
38
39
# File 'lib/util.rb', line 32

def get_product
    {
        'name' => M2M::NAME,
        'version' => M2M::VERSION,
        'homepage' => M2M::HOMEPAGE,
        'repos' => M2M::REPOS
    }
end

#get_relative_dot(relative_url) ⇒ Object

获取一个相对路径离root有几个..



59
60
61
62
63
# File 'lib/util.rb', line 59

def get_relative_dot(relative_url)
    depth = relative_url.split('/').length - 1
    return './' if depth == 0
    return '../' * (relative_url.split('/').length - 1)
end

#get_relative_path(target, source = Dir::pwd) ⇒ Object

获取相对路径,如果没有设定source,则使用当前的工作目录



73
74
75
76
77
# File 'lib/util.rb', line 73

def get_relative_path(target, source = Dir::pwd)
    target = Pathname.new(target) if target.class == String
    source = Pathname.new(source) if source.class == String
    target.relative_path_from(source).to_s
end

#get_temp_dirObject

获取 #################### 临时目录



51
52
53
54
55
56
# File 'lib/util.rb', line 51

def get_temp_dir
    dir = File.join(Dir.home, ".m2m")
    #如果不存在则创建一个
    Dir::mkdir(dir) if(!File::exists?(dir))
    dir
end

#is_config_file?(file) ⇒ Boolean

判断 #################### 判断一个文件是否为配置文件

Returns:

  • (Boolean)


97
98
99
100
# File 'lib/util.rb', line 97

def is_config_file?(file)
    filename = self.get_relative_path file, @workbench
    filename == @config_file
end

#is_markdown_file?(file) ⇒ Boolean

检查一个文件是否为markdown

Returns:

  • (Boolean)


102
103
104
# File 'lib/util.rb', line 102

def is_markdown_file?(file)
    (/\.(md)|(markdown)$/i =~ file) != nil
end

#is_shadow_file?(file) ⇒ Boolean

检查文件是否为.开头的文件

Returns:

  • (Boolean)


107
108
109
# File 'lib/util.rb', line 107

def is_shadow_file?(file)
    (/^\./ =~ file) != nil
end

#write_file(file, content) ⇒ Object

操作 ####################



81
82
83
84
85
86
87
88
# File 'lib/util.rb', line 81

def write_file(file, content)
    dir = File::dirname file
    #如果不在存文件夹, 则先创建
    # puts dir
    FileUtils.mkpath(dir) if not File::exists?(dir)
    #写入文件
    IO.write(file, content)
end