使用 Grunt 管理 AngularJS 个模块版本和发布
Manage AngularJS modules version and releases using Grunt
目前,我有多个 angular 模块。自定义 Grunt 任务连接、缩小和打包每个模块,以便准备部署。我唯一还没有做的就是管理这些模块的版本。
每个项目(每个模块一个)包含一个 package.json 文件,我在其中声明组件的名称和版本 :
{
"name": "my-module",
"version" : "1.0.0",
// etc.
}
所以每个模块都建立在一个目录*/dist/my-module/1.0.0/
但是,在模块本身中,我需要访问它的版本。例如,在控制器中,我声明了一个变量$scope.version = '1.0.0'
。但目前,它被硬编码在控制器脚本中。
第一个问题,有没有办法让模块从package.json文件中获取版本?或者构建应用程序的 grunt 任务将脚本中的给定标志替换为模块的当前版本? (例如,我可以声明我的变量 $scope.version = 'FLAG_VERSION'
知道在构建期间 grunt 将用正确的值替换标志)
第二个问题,是否有一些 grunt 组件允许在我的 VCS(例如 SVN)中标记模块的当前版本,然后递增当前版本?简而言之,执行模块的发布。
编辑: 关于此事的新问题,请参阅
任何帮助或引导将不胜感激!
第一题
我在我的 grunt 构建中添加了一个 grunt-ng-constant 任务(谢谢 @Joe)以从 package.json 文件生成 angular 常量.效果很好,我现在可以将它们注入我的指令和服务中。不过,我必须将模块名称添加到我的 package.json 中,因为 ng-constant 任务需要它来识别生成常量的模块。 package.json中声明的名称与其说是模块名称,不如说是项目名称。
{
"name": "my-project",
"version" : "1.0.0",
"moduleName": "myModuleName"
// etc.
}
低于 ng-constant 任务的结果:
angular.module('myModuleName')
.constant('appInfo', {name:'my-project',version:'1.0.0'})
;
我将我的 grunt 构建任务更改为在我的应用程序脚本末尾连接这个生成的脚本,然后再缩小。所以现在我可以在服务中使用 appInfo,例如:
angular.module('myModuleName').service('myService', [ ..., 'appInfo', function( ..., appInfo ){
// I can use appInfo.name and appInfo.version here !
}
Be careful! I had a surprise applying this to several modules on the same page : if a constant with the same name is declared on two different modules, the value of the former will be overriden by the latter.
第二题
关于那个问题,我问了另一个更准确的问题:。
目前,我有多个 angular 模块。自定义 Grunt 任务连接、缩小和打包每个模块,以便准备部署。我唯一还没有做的就是管理这些模块的版本。
每个项目(每个模块一个)包含一个 package.json 文件,我在其中声明组件的名称和版本 :
{
"name": "my-module",
"version" : "1.0.0",
// etc.
}
所以每个模块都建立在一个目录*/dist/my-module/1.0.0/
但是,在模块本身中,我需要访问它的版本。例如,在控制器中,我声明了一个变量$scope.version = '1.0.0'
。但目前,它被硬编码在控制器脚本中。
第一个问题,有没有办法让模块从package.json文件中获取版本?或者构建应用程序的 grunt 任务将脚本中的给定标志替换为模块的当前版本? (例如,我可以声明我的变量 $scope.version = 'FLAG_VERSION'
知道在构建期间 grunt 将用正确的值替换标志)
第二个问题,是否有一些 grunt 组件允许在我的 VCS(例如 SVN)中标记模块的当前版本,然后递增当前版本?简而言之,执行模块的发布。
编辑: 关于此事的新问题,请参阅
任何帮助或引导将不胜感激!
第一题
我在我的 grunt 构建中添加了一个 grunt-ng-constant 任务(谢谢 @Joe)以从 package.json 文件生成 angular 常量.效果很好,我现在可以将它们注入我的指令和服务中。不过,我必须将模块名称添加到我的 package.json 中,因为 ng-constant 任务需要它来识别生成常量的模块。 package.json中声明的名称与其说是模块名称,不如说是项目名称。
{
"name": "my-project",
"version" : "1.0.0",
"moduleName": "myModuleName"
// etc.
}
低于 ng-constant 任务的结果:
angular.module('myModuleName')
.constant('appInfo', {name:'my-project',version:'1.0.0'})
;
我将我的 grunt 构建任务更改为在我的应用程序脚本末尾连接这个生成的脚本,然后再缩小。所以现在我可以在服务中使用 appInfo,例如:
angular.module('myModuleName').service('myService', [ ..., 'appInfo', function( ..., appInfo ){
// I can use appInfo.name and appInfo.version here !
}
Be careful! I had a surprise applying this to several modules on the same page : if a constant with the same name is declared on two different modules, the value of the former will be overriden by the latter.
第二题
关于那个问题,我问了另一个更准确的问题: