Write Git info strings to exe file C# - c#

I need to write git information: Commit name, tags, branch, etc
to a binary (exe) file.
It is needed to run strings * | grep git, on that file to simply view latest git info about it.
I know that AssemblyInfo is displayed when I run this command (without grep), but I'm facing two possible problems:
I'm not sure if I can write to AssemblyInfo from code, to run 'git log' command and then get its output and write into AssemblyInfo.
If I want to use command 'git log' then it's not going to work on Windows, but it is possible that my App will be built on Windows and not on unix environment.
Maybe you know another way to do that?

This is usually done in your build script, filling a templated version of your AssemblyInfo (look at mustache, a templating language).
This build step does the following:
get the necessary info with git plumbing commands (e.g. How to programmatically determine the current checked out Git branch)
generate the AssemblyInfo file using a template.
Then your software build continues as usual.

There is a .net wrapper for Git windows implementation. Some high profile projects use this implementation. https://github.com/libgit2/libgit2sharp, http://libgit2.github.com/

Related

Wix resource harvesting tool "Heat". How to harvest files with dynamic name like Microsoft run time files generated on build

I have really simple issue but seems I can't find answer easily due Wix restrictions, also to mention I am pretty new to Wix.
Problem is, as described in title, I can't make heat harvest couple of files that are generated on build and name of those files are changed based on OS run time (precisely .Net Core) other then going manually to product.wix file and changing name of couple of resources.
File example
\obj\Release\netcoreapp2.2\win-x64\PubTmp\Out\mscordaccore_amd64_amd64_4.6.28207.03.dll".
Version 4.6.28207.03.dll is dynamical generated.
You can have a try harvesting the outputs of your vs project, since the dll files are the output of your project.
heat project "MyProject.csproj" -pog:Binaries -ag -template:fragment -out project.wxs
And you can also try adding a post-build event on your main application project to copy those files in a special location and have the Heat.exe read them from there. You can refer to this blog here.
In azure devops pipeline, You can try adding a copy file task to copy those files to a specific location where Heat can read from.

Git ignore changes to file but keep it in the repo

This is a git question concerning our C# Xamarin project.
We have a couple files in an Assets folder that are copied in there by a pre-build event command. The files are not used directly by the compiler but if they are missing our CI server will fail the build. The versions of them that are on all the dev machines are probably different so whenever we build on our machines the files get copied in and git shows that they are changed
It's annoying because changes to these files really don't matter so we never need to commit them. I want to ignore changes to these files but not remove them from the repo. If they are removed as mentioned above CI will fail. If I straight up "remove and stop tracking" them they will be deleted from the CI server when it pulls.
From other stack overflow questions I have tried both of these:
git update-index --assume-unchanged [path]
git update-index --skip-worktree [path]
Both succeed in hiding the file changes but both present the following problem. When I try to switch branches it tells me I must discard unstaged changes before switching and lists those files. I then type:
git reset -- .
git checkout .
Both fail to discard the changes to the files until I reverse the update-index commands, only then can I discard the changes and be allowed to switch branches.
Is there a solution to this? Are we doing something outside the box or just plain wrong?
This is a very common problem.
Sometimes it's the well known custom.js, custom.min.js, custom.css, custom.min.css, and the likes. Other times it's another kind of "source files" that somehow needs to be "there" in order for software to work, but we dont really care about what's in them.
Sounds familiar? It's called compiled files. Now I know they are not actually compiled files - they are source files. But for all intents and puposes these files are pretty much the same as compiled files. They are not really anything you care about, and would rather the computer just figured out how to maintain them.
Why treat them any different, than actual compiled files? You mention a pre-build event command makes these files every time you build. That practically makes them part of the build process.
The solution is rather straightforward. On the CI server include the pre-build event that generates the files, before the actual compiling, and add the files to the .gitignore file.

How to change variables of a .exe file?

I want my program in Visual C# to allow the user to generate a portable .exe file to be run on another user's computer, with the portabe .exe receiving the values the first user inserted. I don`t want to use a configuration file or any other file to store the input values to be then read because I want the .exe to be a single file. I have the main and the second program (future portable executable)
as different projects on the same solution. Is there a way to modify values of the portable .exe or does a new program needs to be compiled on the fly?
To formally answer the question.
You can convert the assembly to IL, search and replace and recompile. However, if you are building this application, you should really consider the need and use case for such very thoroughly.
If you need just to modify the behavior of an application, you have a plethora of options.
Registry (can be remotely done)
A config file (though you ruled this out)
You could use a login method to a resouce
Wcf for instance
Active Directory
A network DB
Rebuild you app for these users
Msbuild with a batch file to tweak something
A pre build script
If you are really tricky you "might" be able to track down the offset of an int and tweak it. However this seems very unreliable
Or if you really have your heart set on this, see the following and associated questions
Modify Emdeded String in C# compiled exe
Is it possible to Add/Remove/Change an embedded resource in .NET DLL?
Update from Ben Voigt Comments
Win32 Resources can be changed after being embedded in an EXE. .NET
System.Resources can only be changed before embedding. C# applications
are compatible with both kinds, but the distinction is very important
The The necessary function is UpdateResource
You can typically append to an .exe without breaking it and then read the data in upon execution.
See: appending data to an exe

ASP.NET Service.exe isn't picked up by GitHub

I'm using the GitHub application for windows to transfer my code between my local and the server. I've made two ASP.net services thus far, which work fine- however my latest c# service's .exe and related files aren't picked up by the GitHub app, meaning when I pull from the server, the .exe of the service isn't available to allow installation. (From the debug folder, the installation.log file is picked up, but not the .exe and some attached .dlls)
I've reviewed the directory, and there's no git.ignore instructing the app to ignore it.
Furthermore, when I make changes to my existing services, their .exe is updated, and picked up by the app and allowed to transfer.
Why doesn't my new service's .exe and related installation files get picked up?
I'm using VS2008 (Don't judge me, it's comfortable).
Any advice appreciated, thanks.
Hey actually something like that happened to me once with the Github for Windows. Well at least for what I understand is that the file doesn't get added to the remote git repo.
I would start the git shell:
Open the repo you're working on >> Tools and Options (Button) >> Open a shell here
There try this to see if file is added to the repo:
git ls-files yourService.exe --error-unmatch
if not try figuring out if it's ignored some how:
git check-ignore -v -- yourService.exe
if you get nothing, it's not ignored. If you do get output it is ignored somehow.
So first if it's ignored force add it manually:
git add -f yourService.exe
Now commit and push it to the server. In case you've never done this manually use:
git commit yourService.exe "This is a commit message for one file only..." (For commiting on a specific file)
or if you want to commit on all changes and added files use:
git commit -a yourService.exe "This is a commit message for all changes and added files..."
In the case it's not even ignored you may have merge problems with the server, in that case you have to to force push to server by using: git push -f <remote> <branch> (e.g. git push -f origin master) which is very well explained by Trev Norris here: Force "git push" to overwrite remote files.
After that it should be solved, I kinda stopped using Github for Windows because of these ignore issues (which might be purposely developed but I do it manually and use www.github.com for visuals).
Hope this helps you.

Copying and renaming resource files with script

I'm currently working on a project with multiple language options. I have loads of resource files with my first language and want to copy these files and add some text to them using some sort of batch script in visual studio.
From within VS it will be hard to achieve. There are many standalone tools to edit .resx files separately (like http://resx.sourceforge.net or http://resxmanager.com). Great help when translation gthe files, but I do not know any capable of scripting/batching the job.
RESX are simple XML files, so I think you'd be quicker to write simple console app to load the XMLs via standard C#/Perl/Ruby/Python libraries, patch the texts, and write them back to files instead of searching for a scriptable tool, but maybe I'm wrong. I'd be happy to see such tool too! :)
-- edit:
With pure .bat batch files, it may be hard, because the Windows Console simply lacks proper string-replace commands and you have to use some tricky commands like see the BAT here.. At least small tool like grep or sed would be handy, and the script would be 100% more flexible.
You can rather easily do it with PowerShell, CygWin, Ruby, etc or even the JScript/VBScript that you should already have installed along with your Windows. See here for a such a script in VBS. It scans only a specific directly witout subfolders, but you can easily mix it with this sample to get recursive directory walking. Btw. VBS is kept in files of such extension: myscript.vbs and may be simply double-clicked or simply run just like .bat or .exe files.
All of this however are more like typical programming.. If you have some complex renames to do and if you dont have any Python/Ruby/blargh at hand, I'd recommend just writing it in C# as the code will be friendlier.

Categories