Sunday, July 13, 2008

Tip: ScriptSharp and $(ScriptPath)

If you're using Script# "Class Library in a Web Site" template, you need to make sure you're Script# Projects are located inside the "bin\Scripts" of the Web site. Yes, you heard me right. If you want a reusable class library or simply want to use the scripts generated on multiple Web projects on the same solution, use either "Class Library" or "Atlas Class Library". I thought I had found a bug when I couldn't find the script files. Here's the complete analysis for the brave: 1) Select Script# "Class Library in a Web Site" template

2) Type the following it the default "Class1.cs" created
    1 // Class1.cs
    2 //
    4 using System;
    5 using System.DHTML;
    6 using ScriptFX;
    7 using ScriptFX.UI;
    9 namespace ThinkFarAhead.WordReporter.TestBed.ScriptsForWeb
   10 {
   12     public class Class1
   13     {
   14         public void Greet()
   15         {
   16             Script.Alert("Hi!");
   17         }
   19         public void Main()
   20         {
   21             new Class1().Greet();
   22         }
   23     }
   24 }
3) Compile and search for the *.js files created. Hey, at least I was searching for it forever! Here's my line of thinking went: If Script# is using MSBuild, the code for calling ssc.exe should be in the project.
   49   <Import Project="$(ProgramFiles)\nStuff\ScriptSharp\v1.0\nStuff.ScriptSharp.targets" />
Well, it does import some MSBuild targets. Let's see what is it:
   32     <ScriptCompilerTask
   33       Sources="@(Compile)"
   34       Resources="@(EmbeddedResource)"
   35       References="@(ReferencePath)"
   36       Defines="$(DefineConstants)"
   37       OutputPath="$(OutputPath)"
   38       ScriptPath="$(ScriptPath)"
   39       LocaleSubFolders="$(LocaleSubFolders)"
   40       ScriptName="$(ScriptName)"
   41       Template="$(TemplateFile)"
   42       CopyReferences="$(CopyReferences)"
   43       CSharpAssembly="@(IntermediateAssembly)"
   44       DocumentationFile="$(DocumentationFile)"
   45       SuppressDocumentation="$(SuppressDocumentation)">
   46       <Output TaskParameter="DebugScriptFile" ItemName="DebugScriptFile" />
   47       <Output TaskParameter="ReleaseScriptFile" ItemName="ReleaseScriptFile" />
   48     </ScriptCompilerTask>
Hm... it seems to be putting the scripts at $(ScriptPath). Let's see what's the path during compilation:
   49   <!-- Added by Vyas to know where Script# is keeping my files-->
   50   <Warning Text="Script Folder: $(ScriptPath)"/>
The project template is showing a value for $(ScriptPath) and I'm sure the compile-time warning added would confirm it
   22     <ScriptPath>..\..\..\App_Scripts\</ScriptPath>
Here's the compiler output:

------ Build started: Project: ThinkFarAhead.WordReporter.Web.Scripts, Configuration: Debug Any CPU ------

C:\Windows\Microsoft.NET\Framework\v3.5\Csc.exe /noconfig /nowarn:0028,1591,1701,1702 /nostdlib+ /errorreport:prompt /warn:4 /doc:..\ThinkFarAhead.WordReporter.Web.Scripts.xml /define:DEBUG /reference:"C:\Program Files (x86)\nStuff\ScriptSharp\v1.0\Framework\sscorlib.dll" /reference:"C:\Program Files (x86)\nStuff\ScriptSharp\v1.0\Framework\ssfx.Core.dll" /reference:"C:\Program Files (x86)\nStuff\ScriptSharp\v1.0\Framework\ssfx.UI.Forms.dll" /debug- /optimize+ /out:obj\Debug\ThinkFarAhead.WordReporter.Web.Scripts.dll /target:library Properties\AssemblyInfo.cs ClientCapabilityDetecter.cs

Compile complete -- 0 errors, 0 warnings

C:\Program Files (x86)\nStuff\ScriptSharp\v1.0\nStuff.ScriptSharp.targets : warning : Script Folder: ..\..\App_Scripts\

Done building project "ThinkFarAhead.WordReporter.Web.Scripts.csproj".

I would have tought it should put it inside the project folder! It's dereferencing it from the project folder and put it on the root for me. Project folder: D:\C#\ , Script output folder: D:\App_Scripts. I had looked at the samples before and it's putting the scripts correctly on the App_Scripts folder. Closer scrutiny this time reveals Nikhi's put the Script Projects "inside" the Web project itself at "bin\Scripts"! No wonder "..\..\..\App_Scripts" works for him.

Oh yes, I'm kicking myself for not figuring this one out much earlier!


rekna said...

I'm trying implement jquery for scriptsharp. jquery has two types of 'get' functions. One of them implement XMLHttpRequest behavior, the others works on a DOMElement collection. I have trouble finding right declarations for these functions:

public class jQuery
public static XMLHttpRequest get(string url); { return null; }
public extern static XMLHttpRequest get(string url, Dictionary data) { return null; }
public static XMLHttpRequest get(string url, Dictionary data, GetCallback callback) { return null; }

public DOMElement[] get() { return null; }
public DOMElement get(double index) { return null; }

When I use one of the static functions, compiles ok, when I use one of the instance functions, I won't compile.

I can't rename them, because that's the name that has to be generated for jQuery...

Brad G said...
This comment has been removed by the author.