 exports.registerPlugin = (silky, pluginOptions)->
   #在build和路由启动的时候，加入系统变量
   silky.registerHook 'route:initial', -> appendSystemVariable silky
-  silky.registerHook 'build:initial', -> appendSystemVariable silky
+  silky.registerHook 'build:initial', ->
+    #build时没有指定环境，则假定为preview环境
+    #如果没有指定环境，则指定为preview环境，以解决上传到预览服务器的路径问题
+    #在正式编译的时候，环境会被设置为production环境
+    silky.options.env = 'preview' if not silky.options.original.env
+    appendSystemVariable silky
 
   silky.registerHook 'route:willResponse', {}, (data, done)->
     url = _url.parse data.request.url

  _.defaults jsonData.global, variables

  #把变量加到less中
  pubLess = "../imgotv-pub/css/";
  pubIncludeLess = "#{pubLess}include/";
  lessData = silky.data.less
  lessData.global = lessData.global || ''

  $('body').append html
  $.html()

#获取环境变量
getVariables = (env, server, project_name)->
  previewServer = "http://#{server}.preview.lab.hunantv.com/#{project_name}/"
  cssHunantv = "http://css.hunantv.com/#{project_name}/"
  imgHunantv = "http://img.hunantv.com/#{project_name}/"
  jsHunantv = "http://js.hunantv.com/#{project_name}/"
  imgDemo = "http://image-demo.lab.hunantv.com/#{project_name}/"
  pubTemplate = "/imgotv-pub/template/"
  compDir = "imgotv-pub/component/"

  data =
    preview:
      __css: "#{previewServer}css/"
      __js: "#{previewServer}js/"
      __img: "#{previewServer}image/"
      __pub_img: "#{previewServer}image/imgotv-pub/"
      __img_demo: imgDemo
      __pub_font: "#{previewServer}/font/"
      __base_url: "/#{project_name}/"
      __pub_css_comp: "#{previewServer}css/#{compDir}"
    development:
      __css: "/css/"
      __js: "/js/"
      __img: "/image/"
      __pub_img: "/imgotv-pub/image/"
      __img_demo: "/image-demo/"
      __pub_font: "/font/"
      __base_url: "/"
      __pub_css_comp: "/imgotv-pub/css/component/"
    production:
      __pub_font: "#{cssHunantv}/font/"
      __css: cssHunantv
      __js: jsHunantv
      __img: imgHunantv
      __img_demo: imgDemo
      __pub_img: "#{imgHunantv}imgotv-demo/"
      __base_url: "/#{project_name}/"
      __pub_css_comp: "#{cssHunantv}#{compDir}"

  variables = data[env]

  _.extend variables,
    __standard_css: "http://css.hunantv.com/standard/"
    __pub_tmpl: pubTemplate
    __project: project_name
    __pub_tmpl_comp: "#{pubTemplate}component/"
    __pub_tmpl_ui: "#{pubTemplate}ui/"
    __pub_tmpl_widget: "#{pubTemplate}widget/"

  variables

#更改config的配置
changeConfig = (silky)->
  #添加路由配置
#  routers = silky.config.routers
#  buildRename = silky.config.rename
#  buildRename.push
#    {
#      source: /^imgotv\-pub[\/|\\]\/(.+)/i, target: '/$1', next: false
#    }

#添加系统变量
appendSystemVariable = (silky)->
  project_name = silky.config.name || _path.basename silky.options.workbench

  variables = getVariables silky.options.env, 108, project_name

  #把变量加到global中去
  jsonData = silky.data.json
