 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

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

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

  cssProduction = "http://css.hunantv.com/#{project_name}/"
  jsProduction = "http://js.hunantv.com/#{project_name}/"
  imgProduction = "http://img.hunantv.com/#{project_name}/"
  pubImgProduction = "#{imgProduction}imgotv-pub/"
  pubFontProduction = "#{cssProduction}imgotv-pub/font/"
  pubCssProduction = "http://css.hunantv.com/imgotv-pub/"
  pubCssCompProduction = "http://css.hunantv.com/standard/"

  pubTemplate = "/imgotv-pub/template/"
  variables =
    #公共的css路径
    __pub_css: if isProduction then pubCssProduction else "/imgotv-pub/css/"
    #css的位置
    __css: if isProduction then cssProduction else "/css/"
    #js路径
    __js: if isProduction then jsProduction else "/js/"
    #图片路径
    __img: if isProduction then imgProduction else "/image/"
    #公共模板
    __pub_tmpl: pubTemplate
    #项目名称
    __project: project_name
    #公共库的图片
    __pub_img: if isProduction then pubImgProduction else "/imgotv-pub/image/"
    #公共库的字体
    __pub_font: if isProduction then pubFontProduction else "/font/"
    #公共模板
    __pub_tmpl_comp: "#{pubTemplate}component/"
    #__pub_tmpl_module: "#{pubTemplate}module"
    __pub_tmpl_ui: "#{pubTemplate}ui"
    __pub_tmpl_widget: "#{pubTemplate}widget/"
    #全局组件的css
    __pub_css_comp: if isProduction then pubCssCompProduction else "/imgotv-pub/css/component/"
    #demo图片的地址
    __img_demo: if isProduction then "http://image-demo.lab.hunantv.com/#{project_name}/" else "/image-demo/"

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

  _.defaults jsonData.global, variables

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