Maybe a dump question. Cake states that its a build automation system that can be written in C#. I'm actually playing around a bit and now want to know if it is possible to call .Net methods in build.cake. At the time I've the following build.cake:
var target = Argument("target", "Default");
Task("NuGet")
.Does(() =>
{
// Get local directory
// Get all packages.config files in local directory
// Call nuget restore for every file
var currentDir = System.IO.GetCurrentDirectory(); // This doesn't work
var allPgkConfigs = System.IO.Directory.GetFiles(currentDir, "packages.config", System.IO.SearchOption.AllDirectories); // This doesn't work
foreach (var pgk in allPgkConfigs)
{
// GetNuGetPackageId(pkg);
}
});
Task("Build")
.Does(() =>
{
MSBuild("MySolution.sln");
});
RunTarget(target);
When calling build.ps1 -target nuget I get the following error:
PS C:\> .\build.ps1 -Target nuget
Preparing to run build script...
Running build script...
Analyzing build script...
Processing build script...
Compiling build script...
Error: C:/Users/Mewald-T550/XAP_Playground/build.cake(6,26): error CS0234: The type or namespace name 'GetCurrentDirectory' does not exist in the namespace 'System.IO' (are you missing an assembly reference?)
As cake already states it can't find System.IO how can I add this reference to cake?
I know that cake offers some build-in file operations, but I want to know how to add .Net Framework methods to the cake script.
Thx
You're calling a method on a namespace
Change
System.IO.GetCurrentDirectory()
to
System.IO.Directory.GetCurrentDirectory()
Tried this script and it worked just fine
var directory = System.IO.Directory.GetCurrentDirectory();
Information(directory);
That said, Cake has several IO abstractions built in
I.e. this will achieve the same:
var allPgkConfigs = GetFiles("./**/packages.config");
foreach (var pgk in allPgkConfigs)
{
// GetNuGetPackageId(pkg);
}
If you just want the current directory you can use
Context.Environment.WorkingDirectory
or just
var curDir = MakeAbsolute(Directory("./"));
Information("Current directory is: {0}", curDir);
You can use a reference directive:
#r "bin/myassembly.dll"
or
#reference "bin/myassembly.dll"
See http://cakebuild.net/docs/fundamentals/preprocessor-directives
Reference directive
The reference directive is used to reference external assemblies for use in your scripts.
Usage
The directive has one parameter which is the path to the dll to load.
#r "bin/myassembly.dll"
or
#reference "bin/myassembly.dll"
Try make it point to C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.IO.dll
Related
I have two dotnetcore2.1 projects. First project calls a method of the second project via reflection.
using System;
using System.Reflection;
namespace experiment1
{
class Program
{
static void Main(string[] args)
{
Type _type = Type.GetType("experiment2.Program");
object _object = Activator.CreateInstance(_type);
MethodInfo info = _type.GetMethod("SecondProjectsMethod");
info.Invoke(_object, new object[]{});
}
}
}
I can't give any reference to the Second Project nor changes its code. How can I make this call successfully without adding a Reference to the First Project? I tried to add records to the first project's deps-file and execute the first program like this:
dotnet exec --depsfile experiment1.deps.json experiment1.dll
It didn't work. Is it even possible to do this by changing deps-file or any other config? Or should I manipulate .dll file somehow? Which direction I should go?
You can manually load the assembly by calling:
Assembly.Load("experiment2");
It should look for the assembly in the current folder, or use the deps file to locate it. After that, you should be able to use Type.GetType just fine.
If you want to specify the full path to the assembly, use AssemblyLoadContext.Default.LoadFromAssemblyPath instead.
You can refer to this page for more information on the different ways of loading an assembly in .net core.
I am trying to use the Cake-Plist addin and received an error that dynamic is not yet implemented in the version of Roslyn that was being used. Then from advise for someone else I was told to try the -Experimental switch. When using the switch I am receiving the following error when it is trying to compile the build script.
Error: Microsoft.CodeAnalysis.Scripting.CompilationErrorException: (2,1): error CS0656: Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'
at Microsoft.CodeAnalysis.Scripting.Script.CompilationError(DiagnosticBag diagnostics)
at Microsoft.CodeAnalysis.Scripting.Script.GetExecutor(CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Scripting.Script.Run(Object globals)
at Microsoft.CodeAnalysis.Scripting.Script.Run(Object globals)
at Cake.Scripting.Roslyn.Nightly.DefaultRoslynNightlyScriptSession.Execute(Script script)
at Cake.Core.Scripting.ScriptRunner.Run(IScriptHost host, FilePath scriptPath, IDictionary`2 arguments)
at Cake.Commands.BuildCommand.Execute(CakeOptions options)
at Cake.CakeApplication.Run(CakeOptions options)
at Cake.Program.Main()
Does anyone know what is causing this error?
A reference needs to be added to Microsoft.CSharp.dll.
#reference "Microsoft.CSharp.dll"
https://gitter.im/cake-build/cake?at=57add5a3364ad7fc5acdb660
I had a similar issue when running it on a Mac (OSX El Capitan).
I couldn't find the Microsoft.CSharp.dll anywhere on the Mac (other than in my MS Windows installation), and didn't want to add it as a dependency into one of my projects just for the sake of getting it to build like this on a mac.
I noticed, however, that Mono.CSharp.dll was being downloaded into the ./tools/Cake folder. This serves roughly the same purpose, so I tried to reference it with
#r "Mono.CSharp.dll"
That didn't work either. But when I changed it to
#r "./tools/Cake/Mono.CSharp.dll"
It worked perfectly.
Now all I need to do is determine which platform it's running on and use the correct
#r "xxx.CSharp.dll"...
You can use reflection instead of dynamic. A little less elegant but avoids issues referencing Microsoft.CSharp.dll and Mono.CSharp.dll.
The example would be written as follows.
#addin "Cake.Plist"
Task("update-ios-version")
.Does(() =>
{
var plist = File("./src/Demo/Info.plist");
var data = DeserializePlist(plist);
var itemPropertyInfo = data.GetType().GetProperty("Item");
itemPropertyInfo.SetValue(data, gitVersion.AssemblySemVer, new[] { "CFBundleShortVersionString" });
itemPropertyInfo.SetValue(data, gitVersion.FullSemVer, new[] { "CFBundleVersion" });
SerializePlist(plist, data);
});
I'm working on a plugin for a existing C# .NET Program. It's structured in a manner where you put your custom .dll file in Program Root/Plugins/your plugin name/your plugin name.dll
This is all working well, but now I'm trying to use NAudio in my project.
I've downloaded NAudio via Nuget, and that part works fine, but the problem is that it looks for the NAudio.dll in Program Root, and not in the folder of my plugin.
This makes it hard to distribute my plugin, because it would rely on users dropping the NAudio.dll in their Program Root in addition to putting the plugin into the "Plugins" folder.
Source:
SettingsView.xaml:
<Button HorizontalAlignment="Center"
Margin="0 5"
Width="120"
Command="{Binding SoundTestCommand,
Source={StaticResource SettingsViewModel}}"
Content="Sound Test" />
SettingsViewModel.cs:
using NAudio.Wave;
.
.
.
public void SoundTest()
{
IWavePlayer waveOutDevice;
WaveStream mainOutputStream;
WaveChannel32 inputStream;
waveOutDevice = new WaveOut();
mainOutputStream = new Mp3FileReader(#"E:\1.mp3");
inputStream = new WaveChannel32(mainOutputStream);
inputStream.Volume = 0.2F;
waveOutDevice.Init(mainOutputStream);
waveOutDevice.Play();
}
How can I get C# to look for NAudio in Program Root/Plugins/my plugin name/NAudio.dll instead of looking for it in Program Root/NAudio.dll ?
I'm using VS Express 2013, Target Framework is 4.5 and Output type is Class Library.
Edit:
I found 2 ways to make this work ( I'm not sure what the pros and cons of both methods are - if anyone knows I would appreciate additional information ).
Using the NuGet Package Costura.Fody.
After installing the NuGet package, I simply had to set all other References "Copy Local" to "False" and then set "Copy Local" for NAudio to "True".
Now when I build, the NAudio.dll is compressed and added to my own DLL.
Using the AssemblyResolver outlined below.
It didn't work right away though, so here is some additional information that may help anyone facing the same issue:
I put Corey's code as he posted it into the Helpers folder.
My entry point is Plugin.cs, the class is public class Plugin : IPlugin, INotifyPropertyChanged
In there, the entry method is public void Initialize(IPluginHost pluginHost), but simply putting PluginResolver.Init(path) did not work.
The host program uses WPF and is threaded and I had to use a dispatcher helper function of the host program to get it to work: DispatcherHelper.Invoke(() => Resolver.Init(path));
As mentioned, I'm currently unsure which method to use, but I'm glad I got it to work. Thanks Corey!
You can use the PATH environment variable to add additional folders to the search path. This works for native DLLs, but I haven't tried to use it for .NET assemblies.
Another option is to add a hook to the AssemblyResolve event on the current application domain and use a custom resolver to load the appropriate assembly from wherever you find it. This can be done at the assembly level - I use it in NAudio.Lame to load an assembly from a resource.
Something along these lines:
public static class PluginResolver
{
private static bool hooked = false;
public static string PluginBasePath { get; private set; }
public static void Init(string BasePath)
{
PluginBasePath = BasePath;
if (!hooked)
{
AppDomain.CurrentDomain.AssemblyResolve += ResolvePluginAssembly;
hooked = true;
}
}
static Assembly ResolvePluginAssembly(object sender, ResolveEventArgs args)
{
var asmName = new AssemblyName(args.Name).Name + ".dll";
var assemblyFiles = Directory.EnumerateFiles(PluginBasePath, "*.dll", SearchOption.AllDirectories);
var asmFile = assemblyFiles.FirstOrDefault(fn => string.Compare(Path.GetFileName(fn), asmName, true) == 0);
if (string.IsNullOrEmpty(asmFile))
return null;
return Assembly.LoadFile(asmFile);
}
}
(Usings for the above: System.IO, System.Reflection, System.Linq)
Call Init with the base path to your plugins folder. When you try to reference an assembly that isn't loaded yet it will search for the first file that matches the base name of the assembly with dll appended. For instance, the NAudio assembly will match the first file named NAudio.dll. It will then load and return the assembly.
No checking is done in the above code on the version, etc. and no preference is given to the current plugin's folder.
I figured out I cannot load one script library from another easily:
module.csx
string SomeFunction() {
return "something";
}
script.csx
ExecuteFile("module.csx");
SomeFunction() <-- causes compile error "SomeFunction" does not exist
This is because the compiler does not know of module.csx at the time it compiles script.csx afaiu. I can add another script to load the two files from that one, and that will work. However thats not that pretty.
Instead I like to make my scripthost check for a special syntax "load module" within my scripts, and execute those modules before actual script execution.
script.csx
// load "module.csx"
SomeFunction()
Now, with some basic string handling, I can figure out which modules to load (lines that contains // load ...) and load that files (gist here https://gist.github.com/4147064):
foreach(var module in scriptModules) {
session.ExecuteFile(module);
}
return session.Execute(script)
But - since we're talking Roslyn, there should be some nice way to parse the script for the syntax I'm looking for, right?
And it might even exist a way to handle module libraries of code?
Currently in Roslyn there is no way to reference another script file. We are considering moving #load from being a host command of the Interactive Window to being a part of the language (like #r), but it isn't currently implemented.
As to how to deal with the strings, you could parse it normally, and then look for pre-processor directives that are of an unknown type and delve into the structure that way.
Support for #load in script files has been added as of https://github.com/dotnet/roslyn/commit/f1702c.
This functionality will be available in Visual Studio 2015 Update 1.
Include the script:
#load "common.csx"
...
And configure the source resolver when you run the scripts:
Script<object> script = CSharpScript.Create(code, ...);
var options = ScriptOptions.Default.WithSourceResolver(new SourceFileResolver(new string[] { }, baseDirectory));
var func = script.WithOptions(options).CreateDelegate()
...
I copied Antlr3.StringTemplate.dll and Antlr3.Runtime.dll from http://www.stringtemplate.org/ to cs-script/lib directory ( http://www.csscript.net/ ) and execute the code below but it says it cannot find the assemblies of Antlr why ? My prog below
//css_reference Antlr3.Runtime.dll;
//css_reference Antlr3.StringTemplate.dll;
using System;
using Antlr.StringTemplate;
using Antlr.StringTemplate.Language;
using System.Windows.Forms;
class Script
{
static public void Main(string[] args)
{
StringTemplateGroup group = new StringTemplateGroup("myGroup", #"C:\Tutorials\stringtemplate", typeof(DefaultTemplateLexer));
StringTemplate helloAgain = group.GetInstanceOf("homepage");
helloAgain.SetAttribute("title", "Welcome To StringTemplate");
helloAgain.SetAttribute("name", "World");
helloAgain.SetAttribute("friends", "Terence");
helloAgain.SetAttribute("friends", "Kunle");
helloAgain.SetAttribute("friends", "Micheal");
helloAgain.SetAttribute("friends", "Marq");
Console.Out.WriteLine(helloAgain.ToString());
}
}
The error means that CS-Script cannot resolve Antlr3.*.dll assemblies.
You need to instruct the script engine where these assemblies may be located and if you do not do this CS-Script will look only in the GAC, your local directory (where the script is) and in the [cs-script]\Lib.
You have a few options to solve the problem:
You can specify assemblies with the absolute path
Add probing directories to the CS-Script global settings or directly to your script.
Register Antrl in GAC.
Have a look at AddingSearchDirs and www.csscript.net/help/Using_.NET_assemblies.html, this will help you to start.