Run script and add files to Swift Package via BuildToolPlugin - swift

Context
I recently took over a Swift project which relies on git submodules. One of my first task was to clean up the project, which means removing submodules, using Swift Packages.
On my way doing this i encounter a tricky submodule. It contains swift files and a WebPack project that's loaded in a WebView. Currently the project that imports this submodule has a custom build phase that runs npm ci + npm build to build the WebPack and copy the files into the app.
Question:
When i switch to using a Swift Package it should manage this custom build phase on its own. Is it possible to run npm ci + npm build in a BuildToolPlugin?
My current state is trying to run a shell script which simply creates a file:
#main
struct TSPlugin: BuildToolPlugin {
func createBuildCommands(context: PackagePlugin.PluginContext, target: PackagePlugin.Target) async throws -> [PackagePlugin.Command] {
let tsPath = context.pluginWorkDirectory
.appending (subpath: target.name)
.appending (subpath: "Seatmap")
print(tsPath)
print("here1")
let scriptPath = context.package.directory.appending(["..", "TSPlugin", "TEst.sh"])
print("here2")
return [
.prebuildCommand(
displayName: "Generating localized strings from source files",
executable: scriptPath,
arguments: [],
outputFilesDirectory: tsPath
)
]
}
}
#!/bin/bash
echo "come on"
touch easypeasy.txt
I rebuilt this in a demo project: https://github.com/Deitsch/SwiftPluginDemo

Related

Parse csproj file using cake's ParseProject

Create minimal repro project:
dotnet new classlib --no-restore --output /tmp/Foo
cd /tmp/Foo
dotnet new tool-manifest
dotnet tool install cake.tool
touch build.cake
Put this minimal script in build.cake:
Task("Default").Does(() => {
var file = "./Foo.csproj";
Information(FileExists(file).ToString());
var props = ParseProject(file);
});
RunTarget("Default");
Run it:
dotnet cake
Result:
========================================
Default
========================================
True
An error occurred when executing task 'Default'.
Error: Failed to parse project properties
Surely this minimal script should succeed - have I made a mistake, or is this a bug?
My environment: linux, dotnet 6.0.302.
Out of box, the ParseProject alias within Cake (version 2.2.0), only knows how to parse Visual Studio Project files that are using the "old" format.
Since the Visual Studio Project file that you are creating is using the "new" format (i.e. it was generated using the dotnet CLI, and is targetting .NET Core), the ParseProject in Cake will not be able to recognise it.
There is however an alternative ParseProject alias in the Cake.Incubator addin that can do what you want. You can make use of this by doing:
#addin nuget:?package=Cake.Incubator&version=7.0.0
Task("Default").Does(() => {
var file = "./cake-test.csproj";
Information(FileExists(file).ToString());
var props = ParseProject(file, "Release");
});
RunTarget("Default");
Result:
========================================
Default
========================================
True
Task Duration
--------------------------------------------------
Default 00:00:00.0496415
--------------------------------------------------
Total: 00:00:00.0496415
At some point, the work in the Cake.Incubator addin will likely be merged into Cake, and will ship as a new version, however, for now, you will need to bring in the addin directly.

obfuscate JS before FireBase Deploy

I have started using Firebase to host an app I am building in js.
I want to be able to obfuscate my java script code before I deploy it to FireBase.
I couldn't find a way of doing this automatically with firebase, so I downloaded the closure compiler, and started writing a deploy.bat batch file that would....
Copy all my code and assets from my public folder to a deploy folder
Obfuscate the JS
Deploy the site to Firebase.
However now I have found out that you cant have separate folders for dev local hosting e.g. (public - using the emulator) and another folder for what should be deployed to firebase.
Could anyone suggest how I can get around this ?
You can split the folders into a "development" and "production" build. However care should be taken to ensure that your tests pass against your production build as well as the development build.
The following folder structure will allow to deploy your obfuscated code by calling npm run build followed by npm run deploy from the project/dev folder in your terminal.
- /project
- /prod
- .firebaserc
- firebase.json
- /public
- ... (obfuscated files)
- /dev
- .firebaserc
- firebase.json
- package.json
- /public
- ... (source files)
In project/dev/firebase.json, merge in this to get firebase deploy to deliberately crash if accidentally called from inside of project/dev:
"hosting": {
"predeploy": "node -e \"process.exit(1)\""
}
In project/dev/package.json, merge in this so that your code is built/obfuscated into the project/prod directory:
"scripts": {
"build": "npm run build-hosting && ...",
"build-hosting": "INSERT-TOOL-HERE --srcDir ./public --outDir ../prod/public",
"deploy": "npm run deploy-hosting && ...",
"deploy-hosting": "pushd ../prod && firebase deploy --only hosting && popd",
}

Deploying .NET Core Application with Windows Compatibility Pack

I'm busy deploying a .NET Core 2.1 application into our testing environment, but I'm getting the following error.
Error:
An assembly specified in the application dependencies manifest (MyApp.deps.json) was not found:
package: 'System.Diagnostics.EventLog', version: '4.5.0'
path: 'runtimes/win/lib/netcoreapp2.1/System.Diagnostics.EventLog.dll'
We are using the Windows Compatibility Pack to access the Event Log.
I have the following item in the dependency Json file:
"System.Diagnostics.EventLog/4.5.0": {
"dependencies": {
"Microsoft.Win32.Registry": "4.5.0",
"System.Security.Permissions": "4.5.0",
"System.Security.Principal.Windows": "4.5.0",
"System.Threading.AccessControl": "4.5.0"
},
"runtime": {
"lib/netstandard2.0/System.Diagnostics.EventLog.dll": {
"assemblyVersion": "4.0.0.0",
"fileVersion": "4.6.26515.6"
}
},
"runtimeTargets": {
"runtimes/win/lib/netcoreapp2.0/System.Diagnostics.EventLog.dll": {
"rid": "win",
"assetType": "runtime",
"assemblyVersion": "4.0.0.0",
"fileVersion": "4.6.26515.6"
}
}
}
Please advise how one should deploy these dependencies. Also, what is the root folder to this relative path runtimes/win/lib/netcoreapp2.0?
We actually found a solution for our scenario:
- Our situation was that we tried to run a netcoreapp based test project on our test agent
- dotnet test on the project file worked
- dotnet vstest sometimes worked on the project output directory (we are not sure why and on which setup)
- dotnet vstest did run into the above error when run into an other directory & downloaded from CI
- dotnet vstest did run into an AssemblyNotFoundException on the test agent (which didn't make any sense for us)
The solution was to use dotnet publish for our test project and use the "self-contained" output to run on the test agent. dotnet publish copied the required runtimes/win/lib/netcoreappX.X/*.dll files into the publish output directory.
After a lot of testing, the key issue seems to be the "RuntimeIdentifiers". There is a visible option for this when you publish, but in order to use it when just building you need to add a couple of tags to your .csproj file.
The first is:
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
This will cause NuGet to retrieve the correct dlls (change the value depending on your needs). For me I was compiling to platform x86. I don't know what NuGet was getting by default, but whatever it was had different file sizes for the same files.
You also should then add this tag:
<SelfContained>false</SelfContained>
or else your build will default to copying the entire framework.
Also note that using the RuntimeIdentifier tag will cause your default output folder to include the value you specified. For example my subfolder became:
Project\bin\x86\Debug\netcoreapp3.1\win-86\
For publishing you should be able to do something similar; the problem will be to match your RuntimeIdentifier to your platform. You shouldn't need to specify SelfContained unless you specifically need to.

Git hook update package json version

In our project we often forget to update version numbers in Package.json file. Ours is a AngularJS project. In our package JSON file we are specifying the below two version information
"version": "1.0.7",
"devVersion": "1.0.4"
Before Merging a branch to develop I want a automated script to update these above two version numbers. I am thinking Git Hooks will help me.
Where can i find the hooks, I am able to see the hooks in my local repo under .git folder. I am confused which hook to use. Searching on Google suggests I have to create hooks on server.
Where can i find them and can i update the above both keys (version and devVersion) ?
Pls suggest the location and hook to use, this will solve a lot of problem.
I am using husky and git-branch-is:
"scripts": {
...
"postmerge": "(git-branch-is master && npm version minor ||
(git-branch-is dev && npm --no-git-tag-version version patch)",
...
},
Read more about npm version
Webpack or Vue.js
If you are using webpack or Vue.js, you can display this in the UI using Auto inject version - Webpack plugin
NUXT
In nuxt.config.js:
var WebpackAutoInject = require('webpack-auto-inject-version');
module.exports = {
build: {
plugins: [
new WebpackAutoInject({
// options
// example:
components: {
InjectAsComment: false
},
}),
]
},
}
Inside your template for example in the footer:
<p> All rights reserved © 2018 [v[AIV]{version}[/AIV]]</p>
You have two kinds of hooks (both present in any .git/hooks folder): server and client hooks.
They are listed in "Customizing Git - Git Hooks"
A merge is a local operation, so if you wanted to automate any process during a merge, you would need a client hook, like a post-commit hook (meaning executed just after creating a merge commit).
If you need to update that file before a merge, you can try a pre-commit hook, and check if a merge is in progress (if not, your pre-commit hook would do nothing since you want to update the versions only before a merge).
You can see in this answer an example of a post-commit hook which generates a version.json file.
If is written in node, but you can write a hook ni any scripting language you want.
With Husky, it's extremely simple:
{
"name": "demo-project",
"version": "0.0.3",
"husky": {
"hooks": {
"pre-commit": "npm --no-git-tag-version version patch && git add ."
}
}
}
Note: I put git add . in the end because after we update package version, we need to stage it

How do I configure the output-path of ember-cli as a setting?

I'm using ember as part of a bigger project and so both dev and production build into a subdirectory somewhere else. Can I specify output-path as a setting rather than on the commnad line?
You could modify your package.json and add in the scripts there such as:
"scripts": {
"buildprod": "ember build --environment=production --output-path=yourProdPath",
"builddev": "ember build --output-path=yourDevPath"
}
And just run them in the cli npm buildprod.
Create a file named .ember-cli inside the app folder and mention the output path. Ember will recognize the path automatically when we do "ember build"
{
"outputPath" : "D:/MyApplication/working/ember"
}