25

I have an open source project in which I'm trying to allow development on both MonoDevelop(including *nix) and Visual Studio. One of my recently discovered requirements is I need to copy an outputted file from one directory to another(relative path).

Windows however has the copy command, while *nix has the cp command. What is the best way to get this to work on both platforms and resolve this difference of commands?

5
  • If you are really adamant about having one script for all, you might want to have a look into cygwin, which provides "a Linux look and feel environment for Windows." see more here: cygwin.com
    – nieve
    Oct 31, 2012 at 16:54
  • 2
    @nieve I have that installed, I just didn't want to force everyone who wants to compile my project on Windows to install it
    – Earlz
    Oct 31, 2012 at 17:14
  • You can add a sort of an installation script (something like rake/make) to set up the environment of the other developers, which will decide post-build script will be used according to the OS. If you look at how to build MonoDevelop, you'll see they're using different mechanisms altogether for different environments/OS.
    – nieve
    Oct 31, 2012 at 17:20
  • 1
    @Earlz Did you ever find a solution here? I literally have one DLL to copy... Forcing everyone to install cygwin just for a copy command is pretty overkill
    – SJoshi
    May 11, 2015 at 21:07
  • @Earlz The best I came up with was to make an alias for xcopy on Linux for 'cp -r' As far as I can tell, xcopy isn't anything my standard linux distro picks up, and I definitely don't have anything similar installed on Ubuntu... Soooo, yeah.
    – SJoshi
    May 11, 2015 at 21:13

3 Answers 3

22

Where possible, if you can lean on built-in MSBuild tasks rather than custom shell scripting, the behavior will generally work on xbuild (and hence MonoDevelop?) without any changes, so no need for platform-specific *proj hacks.

eg:

 <Target Name="AfterBuild">
          <Copy SourceFiles="foo.txt" DestinationFolder="$(OutDir)" />
 </Target>

This is from the mono docs: http://www.mono-project.com/archived/porting_msbuild_projects_to_xbuild/#prepostbuildevents

18

You can use the $OS variable to have different post build events depending on the environment. To do this, you must edit the csproj by hand, like:

<PostBuildEvent Condition="'$(OS)' == 'Windows_NT' ">
    dir
</PostBuildEvent>
<PostBuildEvent Condition="'$(OS)' != 'Windows_NT'">
    ls
</PostBuildEvent>
3
  • No Visual Studio integration for this Oct 31, 2019 at 8:43
  • @DanielDäschle - what do you mean by that? Does it make this answer not useful somehow?
    – StingyJack
    May 21, 2021 at 16:53
  • i can't remeber what i meant. Just ignore it :D May 23, 2021 at 14:45
1

You could write the post build script in a language like Python. Or you could require other developers to install GnuWin32 CoreUtils as an option to installing CygWin. CoreUtils includes cp. Then you can just unconditionally use cp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.