适用于Cordova 2015 Update 5的Visual Studio工具中的BeforeBuild和AfterBuild事件

时间:2016-02-09 10:38:50

标签: visual-studio-cordova post-build-event taco

我正在尝试在Visual Studio 2015(TACO)项目中使用前置和后置构建。正如PreBuildEvent and PostBuildEvent on Visual Studio 2015 Tools for Apache Cordova中所述,我已将<Target>个元素添加到我的.jsproj文件中,以便它现在如图所示结束:

  <Import Project="_apacheCordovaProjectSourceItems.Targets" Condition="Exists('_apacheCordovaProjectSourceItems.Targets')" />
  <Target Name="BeforeBuild">
    <Exec Command="if $(ConfigurationName) == Release (echo ** Before Build **)" />  
    <Exec Command="attrib -R &quot;$(ProjectDir)\platforms\*.*&quot; /S" IgnoreExitCode="true" />
  </Target>
  <Target Name="AfterBuild">
    <Exec Command="if $(ConfigurationName) == Release (echo ** After Build **)" />  
    <Exec Command="if $(ConfigurationName) == Release (xcopy &quot;$(TargetDir)*.*&quot; &quot;$(SolutionDir)..\..\Binaries\$(PlatformName)\*.*&quot; /Y /S /E /F /I)" />
  </Target>
</Project>

我的问题是BeforeBuild和AfterBuild事件都在构建开始时触发

1>------ Build started: Project: MyProject, Configuration: Release Android ------
1>  ** Before Build **
1>  ** After Build **
1>  D:\Workspaces\Products\MyProduct\Projects\Main\Sources\Apps\MyProject\bin\Android\Release\android-release-unsigned.apk -> D:\Workspaces\Products\MyProduct\Projects\Binaries\Android\android-release-unsigned.apk
1>  D:\Workspaces\Products\MyProduct\Projects\Main\Sources\Apps\MyProject\bin\Android\Release\manifest-merger-release-report.txt -> D:\Workspaces\Products\MyProduct\Projects\Binaries\Android\manifest-merger-release-report.txt
1>  2 File(s) copied
1>  Your environment has been set up for using Node.js 0.12.2 (ia32) and npm.
1>   ... [Rest of output omitted] ...

有人可以解释为什么会这样,或者我如何在构建完成后运行post build事件?

1 个答案:

答案 0 :(得分:3)

将我的头撞到墙上一段时间之后,我放弃了Visual Studio AfterBuild事件,并使用了一个挂钩用于Cordo after_build。它在整个构建过程中稍早点火,但足以满足我的要求。我将发布其所需要的要点,以防其他人需要做类似的事情。

  1. 在Solution Explorer中找到config.xml,右键单击它并选择View Code
  2. 在config.xml中添加<hook>部分,如下所示

    <platform name="android">
        <hook type="after_build" src="scripts/afterbuild-copy-to-drop.js" />
    </platform>
    

    在这里,我只是挂钩了一个Android版本的after_build事件。

  3. 现在在项目的根目录创建一个脚本文件夹,即与插件和www文件夹相同的级别。

  4. 在这里创建一个JavaScript文件,其名称与钩子定义中的src属性相匹配,即'afterbuild-copy-to-drop.js'。
  5. 在此脚本文件中编写所需的代码。这是我的

    module.exports = function (ctx) {
        console.log('Executing custom "' + ctx.hook + '" hook for ' + ctx.opts.platforms);
    
        var path = ctx.requireCordovaModule('path'),
            shell = ctx.requireCordovaModule('shelljs');
    
        // make sure we are in a release build
        var isRelease = (ctx.cmdLine.indexOf('--configuration Release') >= 0);
    
        var solutionRoot = path.join(ctx.opts.projectRoot, '../..');
        var dropRoot = path.join(solutionRoot, '../../Binaries/Release/Apps');
    
        if (isRelease){
            if (ctx.opts.platforms == 'android') {
    
                var platformRoot = path.join(ctx.opts.projectRoot, 'platforms/android');
                var apkFileLocation = path.join(platformRoot, 'build/outputs/apk/android-release.apk');
    
                dropRoot = path.join(dropRoot, 'Android');
                var dropApkFileLocation = path.join(dropRoot, 'my-app.apk');
    
                console.log('------ Making directory \'' + dropRoot + '\'');
                shell.mkdir('-p', dropRoot);
    
                console.log('------ Copying \'' + apkFileLocation + '\' to ' + dropApkFileLocation + '\'');
                shell.cp('-f', apkFileLocation, dropApkFileLocation);
            }
        }
    
        console.log('Finished executing "' + ctx.hook + '" hook for ' + ctx.opts.platforms);
    };
    
  6. 有关挂钩的更多信息,请访问https://cordova.apache.org/docs/en/dev/guide/appdev/hooks/