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.



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

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.



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

def config_file
  @config_file
end

#encrypt_keyObject (readonly)

Returns the value of attribute encrypt_key.



18
19
20
# File 'lib/util.rb', line 18

def encrypt_key
  @encrypt_key
end

#local_theme_dirObject (readonly)

Returns the value of attribute local_theme_dir.



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

def local_theme_dir
  @local_theme_dir
end

#project_nameObject (readonly)

Returns the value of attribute project_name.



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

def project_name
  @project_name
end

#workbenchObject

Returns the value of attribute workbench.



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

def workbench
  @workbench
end

Instance Method Details

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



128
129
130
# File 'lib/util.rb', line 128

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

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

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



124
125
126
# File 'lib/util.rb', line 124

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

#error(log, level = 1) ⇒ Object

错误



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

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

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

合并两个路径



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

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

获取产品相关的信息



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

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

#get_relative_dot(relative_url) ⇒ Object

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



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

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,则使用当前的工作目录



79
80
81
82
83
# File 'lib/util.rb', line 79

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

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



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

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)


109
110
111
112
# File 'lib/util.rb', line 109

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

#is_markdown_file?(file) ⇒ Boolean

检查一个文件是否为markdown

Returns:

  • (Boolean)


114
115
116
# File 'lib/util.rb', line 114

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

#is_shadow_file?(file) ⇒ Boolean

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

Returns:

  • (Boolean)


119
120
121
# File 'lib/util.rb', line 119

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

#read_file(file) ⇒ Object

读取文件



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

def read_file(file)
    IO.read file, :encoding => 'utf-8'
end

#themes_dirObject

获取m2m自带theme的目录



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

def themes_dir
    File::join(Pathname.new(File.dirname(__FILE__)), 'themes')
end

#write_file(file, content) ⇒ Object

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



87
88
89
90
91
92
93
94
# File 'lib/util.rb', line 87

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