TFS Build 2010 - 部署前缩小 JS/CSS

发布于 2024-12-05 13:14:06 字数 680 浏览 7 评论 0 原文

我已经掌握了 Ajax 压缩器,以便在构建/打包/部署过程中压缩一些 JS 和 CSS 文件。它是一个很棒的工具,并且完全满足我们的需要。然而,事实证明,将其集成到我们的构建/部署过程中非常困难。

理想情况下,我们希望在执行 TFS 2010 构建之一时运行此工具(即在开发计算机上本地 (Ctrl+Shift+B jobbie) 构建) )。另外,我们希望在这种情况下用缩小的文件(即在相同的文件名下)替换当前的“非缩小”文件,而不是加载名为“.min.js”等的附加文件

。阅读我认为关键是工作流程中的自定义构建任务 - 但我不知道如何解决这个问题 - 特别是当我希望缩小直接从 TFS 中的发布分支拉下的文件时(即不在某人的本地工作区)作为 TFS 2010 构建的一部分。

这是我发现的与我想要实现的目标最接近的讨论: Microsoft Ajax Minifier - TFS 2010 工作流程 - TFS 构建中的 AjaxMin

我相信我需要在构建工作流程,但不知道如何创建一个来解决这个问题。任何人都可以阐明允许在部署之前进行缩小的流程吗?

I've got hold of the Ajax minifier in order to minify some JS and CSS files as part of a build/package/deploy process. It's a great tool and does exactly what we need. However, integrating this into our build/deploy process is proving very difficult.

Ideally, we want to run this tool only when we execute one of our TFS 2010 builds (i.e. NOT a local (Ctrl+Shift+B jobbie) build on a dev machine). Also, we want to replace our currently 'non-minified' files in this scenario with the minified ones (i.e. under the same file name) rather than having a load of additional files named '.min.js' etc.

After a lot of reading I think the key is for a custom build task within the workflow - but I've no idea on how to approach this - espeically as I'm looking to minify files that will have been pulled down directly from our release branch in TFS (i.e. not in someone's local workspace) as part of a TFS 2010 build.

This is the closest discussion I've found to what I'm trying to achieve: Microsoft Ajax Minifier - TFS 2010 Workflow - AjaxMin in the TFS Build

I beleive I will need a custom code activity within the build workflow but have no idea on how to create one to solve this problem. Can anyone shed any light on a process which will allow minification prior to deployment?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼中杀气 2024-12-12 13:14:07

好的 - 感谢这些答案和大量的研究...我得出了以下解决方案:)

从自定义代码活动开始,我尝试从 C# 代码运行缩小器,然后将该活动作为工作流程的一部分调用。这不起作用,因为 .dll 版本的压缩器公开了几种压缩 .js 和 .css 文件的方法,然后让您打开某种 StreamWriter 并使用从返回的压缩字符串重写文件方法(如果您想覆盖现有文件)。一整天都需要大量打开和关闭文件,因此我对该解决方案不太满意。使用进程类在启用 -clobber 选项(用于覆盖文件)的情况下运行 .exe 也不理想,并且会产生一些奇怪的结果(未正确缩小文件并在每个文件的头部写入一些垃圾)。

所以,你问,我决定的解决方案是编写一个 PowerShell 脚本(我从 这里得到的开始 - 然后我对其进行了稍微修改以采用命令行参数 - 这将是项目的根文件夹 该脚本递归地遍历每个文件(以及每个子目录的文件)并缩小了里面的 .css 和 .js ,其结构看起来像这样:

$ScriptDirectory = $args[0]
Write-Host "Validating directory parameter: $ScriptDirectory"
Write-Host ""

if ((Test-Path -path $ScriptDirectory) -ne $True)
{
     #Throw an error of some kind (the parameter passed in isn't a valid directory).
}

$Minifier = “C:\Program Files\Microsoft\Microsoft Ajax Minifier 4\AjaxMin.exe”

get-childitem $ScriptDirectory -recurse -force -include *.js, *.css -exclude *.min.js, *.min.css | foreach-object {&$Minifier $_.FullName -out $_.FullName -clobber}

所以我们使用 .js 或 .css 扩展名来遍历根文件夹的每个子项目(忽略 .min.* 的扩展名)这些已经被压缩)。

在 TFS 中,我们需要做的就是向 InvokeProcess 步骤。 href="http://www.ewaldhofman.nl/post/2010/11/09/Part-14-Execute-a-PowerShell-script.aspx" rel="noreferrer">在 TFS 中执行 PowerShell 脚本 您可以使用 InvokeProcess 活动的 Arguments 属性将参数传递到(开始压缩的目录)。

要在发布之前获取 TFS 构建用于编译代码的目录(临时工作区,如果您愿意),您可以使用构建的 Run On Agent 序列中可用的 SourcesDirectory 变量。这是 TFS 构建过程编译和打包文件的位置,因此此处缩小的任何内容都将最终出现在最终的部署包中。

PS - SourcesDirectory 非常高 - 您可能不想从那里一直钻取到您的 .js 和 .css 文件,因此您需要指定类似以下内容:

SourcesDirectory + "/" + "MyProjectFolder/Scripts"

确保在代码执行之前添加此 InvokeProcess 步骤部署在工作流程中,嘿-presto - 您将拥有缩小的 .js 和 .css 文件,它们仅将原始文件名保留为 TFS 构建的一部分,而不是本地文件名。

非常感谢所有回答并为我指明正确方向的人。我希望这对一路上的人有所帮助!

OK - thanks to these answers and a lot of research... I came to the following solution :)

Starting with custom code activities, I tried to run the minifier from C# code and then called the activity as part of the workflow. This didn't work as the .dll version of the minifier exposes a couple of methods for compressing both .js and .css files and then makes you open up a StreamWriter of some kind and re-write the file with the compressed string returned from the method (if you're wanting to overwrite your existing files). Pretty intensive opening up and closing files all the day so I wasn't massively happy with that solution. Using the process class to run the .exe with the -clobber option on (for overwriting files) isn't ideal either and produced some odd results (not correctly minifying the files and writing some garbage at the head of each file).

So, you ask, the solution I settled on was to write a PowerShell script (the beginnings of which I got from here - which I then modified slightly to take a command-line parameter - which would be the root folder of your project. The script recursively goes through each file (and each sub-directory's files) and minifies the .css and .js inside. Pretty neat. The bones of which looks something like this:

$ScriptDirectory = $args[0]
Write-Host "Validating directory parameter: $ScriptDirectory"
Write-Host ""

if ((Test-Path -path $ScriptDirectory) -ne $True)
{
     #Throw an error of some kind (the parameter passed in isn't a valid directory).
}

$Minifier = “C:\Program Files\Microsoft\Microsoft Ajax Minifier 4\AjaxMin.exe”

get-childitem $ScriptDirectory -recurse -force -include *.js, *.css -exclude *.min.js, *.min.css | foreach-object {&$Minifier $_.FullName -out $_.FullName -clobber}

So we go through each child item of the root folder with an extension of .js or .css (ignoring extensions of .min.* as these have already been compressed).

In TFS, all we need to do is add an InvokeProcess step to execute the PowerShell script in TFS. You can pass your parameter in (the directory to start minifiying) using the Arguments property of the InvokeProcess activity.

To get the directory that TFS build is using to compile your code before it is released (the temporary workspace, if you like), you can use the SourcesDirectory variable available to you in the Run On Agent sequence of the build. This is the location that your files are compiled and packaged up by the TFS build process so anything that gets minified here will end up in the final deployment package.

P.S - the SourcesDirectory is quite high up - you might not want to drill all the way from there to get to your .js and .css files so you mgiht need to specify something like:

SourcesDirectory + "/" + "MyProjectFolder/Scripts"

Make sure you add this InvokeProcess step before your code is deployed in the workflow and hey-presto - you'll have minified .js and .css files, which keep the original file names only as part of a TFS build and not a local one.

Many thanks to all who answered and pointed me in the right direction. I hope this helps someone along the way!

心欲静而疯不止 2024-12-12 13:14:07

作为自定义代码活动来执行此操作当然是可行的,但您必须付出一些努力。我的建议是:

  1. 首先关注 Ewald Hofman 的 TFS 2010 自定义博客 http://www.ewaldhofman.nl/post/2010/04/29/Customize-Team-Build-2010-e28093-Part-4-Create-your-own-activity.aspx 了解如何创建自定义活动
  2. 然后查看 http://www.ewaldhofman.nl/post/2010/06/01/Customize-Team-Build-2010-e28093-Part-10-Include-Version-Number-in-the-Build- Number.aspx 来自同一系列,用于实现查找所有符合某种模式的文件的机制。在该示例中,对 assemblyInfo.cs 文件进行了索引,并且其内容发生了更改。通过搜索您的 *.js 文件来替换它。
  3. 在您选择的文件上释放 Ajax Minifier 的强大功能,并用缩小后的文件替换原始文件。
  4. 构建活动,将其作为您在 TFS 2010 中使用的构建过程模板中的一个步骤(也在相同的博客文章中进行了描述)并将其微调到您满意的程度。

或者,您可以要求问题中包含的帖子的作者分享他与我们创建的 Minifier TFS 活动:-)

请告诉我这对您来说效果如何。

Doing this as a custom code activity is certainly doable, but you'll have to put some effort in it. My suggestion would be:

  1. First follow the TFS 2010 customisation blog from Ewald Hofman at http://www.ewaldhofman.nl/post/2010/04/29/Customize-Team-Build-2010-e28093-Part-4-Create-your-own-activity.aspx to learn aboutr creating custom activities
  2. Then take a look at http://www.ewaldhofman.nl/post/2010/06/01/Customize-Team-Build-2010-e28093-Part-10-Include-Version-Number-in-the-Build-Number.aspx from the same series to implement a mechanism to find all files that conform to a certain pattern. In that example, assemblyInfo.cs files are indexed and their content changed. Replace that by searching for your *.js files.
  3. Release the power of Ajax Minifier on your file selection and replace the original file with the minified one.
  4. Build the activity, include it as a step in the build process template you use in TFS 2010 (also described in the same blog posts) and fine tune it to the point you are satisfied with it.

Alternatively, you can ask the author of the post you have included in your question to share the Minifier TFS activity he created with us :-)

Please, let me know how that works out for you.

相对绾红妆 2024-12-12 13:14:07

您需要实现一个Invoke Process Activity,以便 Minifier 在 TFS 构建期间执行。

为此,您还必须在进行构建的服务器中安装压缩器,即所谓的构建代理。通过这样做,您将确保仅在 TFS 构建期间(而不是本地 VS 构建)调用 Minifier。

为了重命名生成的输出文件 (*.min.js),您需要为此实现另一个自定义活动。
覆盖签入的文件需要您首先使它们可写,这意味着另一个自定义活动(我在 另一个答案 一个片段)。

整个编舞是
使用 InvokeProcess 调用缩小器 --> 使签入的文件可写 --> 用重命名的缩小文件覆盖签入的文件

在 TFS 构建中执行此操作的正确方法是将它们包装在 Sequence 中。

可以找到一篇关于如何实现 Invoke Process 的优秀介绍性博客文章 此处
我还找到了 E.Hofman 的系列,具有真正的价值。

You need to implement an Invoke Process Activity so that Minifier gets executed during your TFS builds.

To this purpose you will also have to install the minifier in the Server(s) doing your builds, so so-called Build-Agent(s). By doing that you 'll be ensuring that the Minifier gets invoked only during your TFS-Builds (as opposed to local VS-Builds).

In order to rename your generated output files (*.min.js) you need to implement another custom activity just for that.
Overwritting your checked-in files needs you to first make them writable, this means yet another custom activity (I 've provided in another answer a snippet for that).

The whole choreography is
Invoke Minifier with InvokeProcess --> make checked in files writable --> overwrite checked in files with renamed minified files.

The right way to do this in TFS build is to wrap them in a Sequence.

A good introductory blog post about how to implement an Invoke Process is to be found here.
I have also found the series by E.Hofman of real value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文