Visual Studio Tools for Cordova 2015 Update 5 中的 BeforeBuild 和 AfterBuild 事件

BeforeBuild and AfterBuild events in Visual Studio Tools for Cordova 2015 Update 5

我正在尝试在我的 Visual Studio 2015 (TACO) 项目中使用预构建和 post 构建。如 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 构建事件发送到 运行?

在用头撞墙一段时间后,我放弃了 Visual Studio AfterBuild 事件,并为 Cordova after_build 事件使用了一个钩子。它在整个构建过程中稍早触发,但足以满足我的要求。我会 post 它的要点,以防其他人需要做类似的事情。

  1. 在解决方案资源管理器中找到 config.xml,右键单击它并 select 查看代码
  2. 在config.xml中添加一个<hook>部分如下

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

    这里我挂接到 after_build 事件,仅用于 Android 构建。

  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);
    };
    

有关挂钩的更多信息,请访问 https://cordova.apache.org/docs/en/dev/guide/appdev/hooks/