module Tcms

Constants

PROJECT_FILE
UPLOAD_CGI
VERSION

Public Class Methods

build_project(type) click to toggle source
# 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_project() click to toggle source
# 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_project() click to toggle source

发布整个项目

# 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) click to toggle source

读取配置文件数据

# File lib/tcms.rb, line 17
def self.read_config_file file=PROJECT_FILE
   YAML.load(File.open(file))
end
upload_assets(files) click to toggle source
# 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) click to toggle source

上传单个文件

# 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