Module: Tcms

Defined in:
lib/tcms.rb,
lib/tcms/helper.rb,
lib/tcms/version.rb,
lib/tcms/publisher.rb

Defined Under Namespace

Modules: Helper Classes: Publisher

Constant Summary collapse

PROJECT_FILE =
"project.yml"
UPLOAD_CGI =
'http://wizard2.webdev.com/tcms/tools/uploadfile.php'
VERSION =
"1.1.4.3"

Class Method Summary collapse

Class Method Details

.build_project(type) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/tcms.rb', line 149

def self.build_project type
  unless type then type = 'dev' end
  unless ['dev','test','rel','release'].include? type
    puts '未知命令选项,请选择[dev,test,release].'
    return
  end


  project = self.read_config_file PROJECT_FILE

  config_file = project["templates"]["folder"]+"/config.js"

  @template_config = Hash.new

  if File.exist? config_file
    puts "读取模版配置文件#{config_file}.."
    config_content = File.read(config_file,:encoding=>"UTF-8")
    @template_config = JSON.parse config_content.gsub(/'/,'"')
  end

  @template_config.each do |key,value|
    if value.respond_to? :has_key? and value.has_key? "_data_from_url"
      puts "获取献上接口数据#{value}.."
      clnt = HTTPClient.new
      response = clnt.get_content value["_data_from_url"]
      @template_config[key] = JSON.parse(response)

      if @template_config[key].is_a?(Hash)
        if @template_config[key].has_key? 'code'
          @template_config[key] = @template_config[key]["data"]
        end
      end
    end
  end

  FileList.new(project['templates']['folder']+'/**/*').each do |f|
    result =  f.match(/\/(\w+).html?\.erb/)
    if result
      publisher = Tcms::Publisher.new(result[1],@template_config,project)
      publisher.render(type)
    end
  end
end

.create_projectObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/tcms.rb', line 193

def self.create_project
  template_folder = Pathname.new(File.dirname(__FILE__)).realpath.parent.to_s+"/template"
  if File.exist? 'project.yml'
    puts '已存在配置文件project.yml'
  else
    puts '创建配置文件成功!-project.yml'
    content = File.read(template_folder+"/project.yml.edit",:encoding=>"UTF-8")
    pro_file = File.new('project.yml',"w+")
    pro_file.puts content
    pro_file.close
  end

  unless File.exist? "server.local"
    server_content = File.read(template_folder+'/server.local',:encoding=>"UTF-8")
    server_file = File.new("server.local","w+")
    server_file.puts server_content
    server_file.close
    File.chmod(0755,'server.local')
  end

end

.deploy_projectObject

发布整个项目



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/tcms.rb', line 119

def self.deploy_project
  unless File.exist? PROJECT_FILE
    puts 'project.yml 不存在,请运行: tcms -create 创建配置文件'
    return
  end

  project = self.read_config_file PROJECT_FILE
  assets = project['assets']
  #上传素材文件夹
  FileList.new(assets["folder"]+'/**/*').each do |f|
    unless File.directory? f
      if f.include? project["version"].to_s
        self.upload_file f,assets
      end
    end
  end
  #上传页面文件
  #TODO fixed concat upload function
  pages = project['pages']
  FileList.new(pages['folder']+"/**/*").each do |f|
    unless File.directory? f
      if f.include? 'htm'
        unless f.include? '__upload__'
          self.upload_file f,pages
        end
      end
    end
  end
end

.read_config_file(file = PROJECT_FILE) ⇒ Object

读取配置文件数据



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

def self.read_config_file file=PROJECT_FILE
   YAML.load(File.open(file))
end

.upload_assets(files) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tcms.rb', line 21

def self.upload_assets files
  project = self.read_config_file()
  files.each do |file|
    file_path = "#{file}/assets.yml"

    unless File.exist? file_path
      puts "#{file}/ 下无assets.yml配置,请配置素材远程频道和目录.."
      next
    end
    file_config = self.read_config_file file_path
    if File.directory? file
      FileList.new(file+'/**/*').each do |f|
        unless File.directory? f
          self.upload_file f,file_config
        end
      end
    elsif File.exist? file
        self.upload_file file,file_config
    else
      puts "#{file} :无此文件"
    end
  end
end

.upload_file(file, config) ⇒ Object

上传单个文件



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/tcms.rb', line 46

def self.upload_file file,config

  file_name = file.sub(config["folder"]+'/','')

  remote_path = config["remotepath"]+'/'+file_name
  puts "正在上传#{file}"
  upload_path = "#{UPLOAD_CGI}?site=#{config["site"]}&channel=#{config["channel"]}&remotepath=#{remote_path}"
  upload_file_path = file

  begin
    if config["remote_encoding"]
      file_content = File.read(file)
      file_content = file_content.encode(config["remote_encoding"],"UTF-8",:invalid=>:replace,:undef=>:replace)

      upload_file_path = config["folder"]+'/__upload__'+file_name
      tmp_file = File.new(upload_file_path,'w+')
      tmp_file.puts file_content
      tmp_file.close
    end
    #puts upload_path
    #puts File.read(upload_file_path)
    File.open(upload_file_path) do |file|
      body = {"NEW_FILE"=>file}
      clnt = HTTPClient.new
      res = clnt.post(upload_path, body)
      #puts res.body.downcase
      if res.status == 200
        puts '--上传成功!'
        site =  config["site"]
        domain = 'qq.com'

        #业务修正
        if site.match 'mat1' then domain = "gtimg.com/#{config["channel"]}" end
        if site.match 'pingjs' then site = 'rss' end

        url = "http://#{site}.#{domain}/#{remote_path}"
        puts '--远程地址:'+url
      else
        raise res
      end
    end

    #response = RestClient.post upload_path,{:NEW_FILE=>File.new(upload_file_path,'rb')}
    #
    # if [200].include? response.code
    #   puts response
    #   #puts '--上传成功!'
    #   site =  config["site"]
    #   domain = 'qq.com'
    #
    #   #业务修正
    #   if site.match 'mat1' then domain = "gtimg.com/#{config["channel"]}" end
    #   if site.match 'pingjs' then site = 'rss' end
    #
    #   url = "http://#{site}.#{domain}/#{remote_path}"
    #   puts '--远程地址:'+url
    # else
    #   raise response
    # end
    #
  rescue Exception => e
    puts ''
    puts "(>_<)..."
    puts "#{file}上传失败!"+ e.message
    puts ''
  ensure
    if upload_file_path.include? '__upload__'
      File.delete upload_file_path
    end
  end
end