Using Compass on Windows with Visual Studio C# and ASP.NET - c#

Has anyone done any development of Compass for CSS/SASS in a standard C# ASP.NET environment?
Is there a single distribution I can just download that's ready to go for Windows or do I need install every piece of the equation and build compass myself?
Are there any plugins that make developing with Compass friendlier with VS2008 such as automagical handling of Compass/SASS in builds, syntax highlighting, and/or intellisense support?
If there aren't any VS IDE plugins what are the best options for a standalone text editor for handling coding in Compass?

To complete the last answers, you can install Web Workbench, a plugin for Visual Studio 2010 wich add syntax highlighting, intellisence and some other stuff for the SASS language (SCSS syntax only).
If you prefer using Compass and/or some other tools to compile your CSS, you should disable the built-in compiler. I listed some other SASS compilers here: Using SASS with ASP.NET.
To disable the built-in compiler: select the .scss file in Solution Explorer, go to the Properties window and delete the text from the Custom Tool box.
Since Web Workbench 3 you can now manage more easily what you want to compile with this plugin. See the Mindscape > Web Workbench Settings menu item.

Getting started with Compass,
First yes I have to install Ruby and the compass source and compile up my version of compass I followed the instructions on Compass's Wiki Getting Started.
After getting Compass and all it's dependencies installed and built I created my first project.
compass -f blueprint project-name
Which creates a default project with compass for the blueprint css framework, currently there's a bug in compass with the creation of the grid.png file in the images directory for compass so you need to copy the original grid.png from the source folder
C:\Ruby\lib\ruby\gems\1.8\gems\chriseppstein-compass-0.8.10
\frameworks\blueprint\templates\project
Or similarly located file depending on where you installed everything. One of the most important changes IMO for working with compass on asp.net is to change the SASS CACHE directive of compass. The SASS CACHE creates a bunch of temporary folders in your project directory which probably would have poor outcomes if they ended under source control. So open up config.rb and add this line
sass_options = {:cache_location =>
"#{Compass.configuration.project_path}\\tmp\\sass-cache"}
Make sure to note the escaped backslashs.
After this I modified the names of the folders that compass uses for how I wanted them named inside the config.rb and started getting to it with SASS and Compass. I recommend watching the hour long introduction to compass video it's very helpful and I learned alot from it: Watch the screen cast.
One of the things which this showed me was how to set compass to watch for file system changes and automagic compile the sass to css. By using
compass -w
This is working real well for me, just make sure you keep your css files checked out or turn them off read only if they're under source control if your project doesn't support concurrent checkouts.
For editing I'm using SciTE that's included with Ruby by default for the config.rb files or just the editor window in VS2008. For Sass I came across a big list on the HAML website. jEdit with highlighting syntax file for SASS was what I ended up using after trying a few. I'd still like to find a VS plugin for syntax highlighting so I don't need to use another editor but jEdit is definitely getting the job done.

My answer is a bit antiquated. Before following my original answer, I would recommend exploring the Nuget package SassAndCoffee. The full details can be found here.
How does it work?
SassAndCoffee embeds the original
compilers in the DLL as (Sass 3.2.0
and CoffeeScript 1.1.0 as of this
writing) and uses IronRuby and
Jurassic respectively to execute the
compilers against your source.
Why is
this better than [SOMEOTHERPROJECT]?
No external processes are executed
You don’t have to install Ruby or node.js
It’s in NuGet so you don’t have to fiddle with web.config
Files are cached and are rebuilt as-needed.

I wanted to add another alternative here. If you just want to ensure that compass builds the sass files and includes the css files when you build your ASP.net project you can add the following to your project(csproj) file under the project section:
<Target Name="AfterBuild" Condition="'$(Configuration)' == 'Release' ">
<Exec Command="compass compile --output-style compressed --force" />
<ItemGroup>
<Content Include="Styles\*.css" />
</ItemGroup>
</Target>
<Target Name="AfterCompile" Condition=" '$(Configuration)' == 'Debug' ">
<Exec Command="compass compile" />
<ItemGroup>
<Content Include="Styles\*.css" />
</ItemGroup>
</Target>
The first Target is for Release and will also compress the css, the other one is for Debug.
If you want to customize paths add a config.rb to the project root folder:
css_dir = "Content"
sass_dir = "Content/Sass"
This all of course requires compass and ruby to be installed and to be in the path of your machine.

Related

Neither PackageIcon nor PackageIconUrl work in dotnet build

I'm using visual studio 2017. I have a project for which I would like to generate a nuget with an Icon.
If I use
...
<PackageIconUrl>http://blabla/icon.png</PackageIconUrl>
I get the following error :
error NU5048: The 'PackageIconUrl'/'iconUrl' element is deprecated. Consider using the
'PackageIcon'/'icon' element instead. Learn more at https://aka.ms/deprecateIconUrl
[D:\myproject.csproj]
Fine, So I change my project to be something like :
...
<PackageIcon>core.png</PackageIcon>
...
<Content Include="..\Shared\core.png" Link="core.png" Pack="true" PackagePath="\" >
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
When I build my project now (dotnet build Myproject.csproj) I don't get any compilation errors.
However, when I want to view the generate nupkg in "Nuget Package explorer". I get the error :
The element 'metadata' in namespace 'http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd'
has invalid child elment 'icon' in namespace 'http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd'.
List of possible elements expected : 'contentFiles, desription, licenseUrl, projectUrl, language,
releaseNotes, frameworkAssemblies, summary, iconUrl, packageTypes, dependencies, copyright,
developmentDependency, repositoru, tags, references, title, serviceable' in namespace
'http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd'.
(so it seems the generated embedded nuspec has a wrong xml namespace - I have no idea how I can make it generate the new namespace) This is a Catch-22.
So it seems the only option is to leave the icon out.
(I also have visual studio 2019 and recently installed the .net core 3.0.100 SDK)
The move from <iconUrl>/<PackageIconUrl> to <icon>/<PackageIcon> is fairly new. While nuget.org supports parsing packages which use <icon> rather than <iconUrl>, some other package servers don't yet.
It sounds like you're simply browsing a directory with the NuGet Package Explorer, so the cause is probably that this doesn't yet understand <icon>.
While <iconUrl> is deprecated, I've found that it still works on nuget.org, so you should be able to continue using it until the rest of the tooling catches up. Alternatively if you don't actually care about browsing packages stored in a local directory and just care about nuget.org, then you can start using <icon>.
You might even be able to use both - I haven't checked.
At least I think you can feel free to use the PackageIconUrl for now, as canton7 mentioned above, the move is fairly new. You can consider that as a kind reminder.
For the latest VS2019 release version 16.3.3, its project template still supports PackageIconUrl by default. For .net core and .net standard class library project, if we Right-click project=>Package tab we can see:
And it is equivalent to <PackageIconUrl>http://xxx/icon.png</PackageIconUrl> in xx.csproj, so I think you can just ignore that warning or use <NoWarn>NU5048</NoWarn>.
You need to make sure that the icon is part of the package. You can verify that it's in there by unzipping your nupkg file.
From a Microsoft site:
<PropertyGroup>
...
<PackageIcon>icon.png</PackageIcon>
...
</PropertyGroup>
<ItemGroup>
...
<None Include="images\icon.png" Pack="true" PackagePath="\"/>
...
</ItemGroup>
Also (from here):
PackageIconUrl is deprecated in favor of PackageIcon. However, for the best downlevel experience, you should specify PackageIconUrl in addition to PackageIcon.
There is also an example project with icon: https://github.com/NuGet/Samples/tree/main/PackageIconExample

How to add Razor file to Xamarin project

I'm trying to get Razor working inside my Xamarin project and I can't seem to get Visual Studio 2017 to recognize that the files need to be processed by the Razor preprocessor.
When I go to add a new file, there's no template for Razor or .cshtml, so I'm just adding a text file and change the extension to .cshtml. Then, in the file properties, I'm setting the Custom Tool to RazorTemplatePreprocessor.
However, despite doing this, Visual Studio does not generate the .cs file that I expect it to.
What am I missing?
UPDATE AND WORKAROUND: As a result of helpful conversation with #SushiHangover (see answer and comment chain below), it appears that .NET Standard library projects do not currently allow Razor files to be added through the Add Item wizard, and if you add them manually, they don't compile correctly. this is true as of Visual Studio 15.5.2. I've opened a bug with Microsoft, but in the meantime, I have super clunky workaround, which is as follows.
At the end of the day, Razor files as you might use them in a Xamarin project are only there to generate .cs files, which compile into classes containing a GenerateString method, which code can call to generate HTML that can then be fed into a WebView. So, I figured, why not just create a regular .NET Framework library (not .NET Standard) and put all my Razor files there. In that project, you can add them normally and they work properly.
But since the rest of my project is .NET Standard and I didn't want to mix and match, I don't actually have any of the other projects reference this library. Instead, I just include the generated .cs files as links (click the little arrow next to the Add button and choose Add as Link instead) in my "real" project (the one where I wanted to add them originally). So the library with Razor files has only one purpose, and that is to generate .cs files, period. Those files are actually compiled into a different library.
I hope this helps someone! When Microsoft fixes the bug, I'll update this thread again (and move my Razor files back to where they ought to be).
EDIT 2
Microsoft has marked my bug "Under Consideration." If this issue affects you, please upvote it to encourage them to fix it sooner rather than later.
https://developercommunity.visualstudio.com/content/problem/172997/razor-templates-not-working-for-net-standard-proje.html
The Razor file templates are available on Visual Studio for Mac (VS4M) under the "Text Templating" group, but the recent versions of Visual Studio for Windows (VS4W) they have gone missing.
On VS4W, you can just edit the .csproj that you are trying to add a Razor file:
A Compile item for the generated .cs file that includes a DependentUpon tag for the .cshtml file.
A None item for the .cshtml that includes the Generator and LastGenOutput tags
Create those two files (.cshtml & .cs) (they can be empty to start)
Example (Xamarin.iOS, Xamarin.Android, & PCL projects)
<ItemGroup>
<Compile Include="RazorTemplate.cs">
<DependentUpon>RazorTemplate.cshtml</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="RazorTemplate.cshtml">
<Generator>RazorTemplatePreprocessor</Generator>
<LastGenOutput>RazorTemplate.cs</LastGenOutput>
</None>
</ItemGroup>
Example (NetStandard 2.0 project)
<ItemGroup>
<Compile Condition=" '$(EnableDefaultCompileItems)' == 'true' " Update="RazorTemplate.cs">
<DependentUpon>RazorTemplate.cshtml</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Update="RazorTemplate.cshtml">
<Generator>RazorTemplatePreprocessor</Generator>
<LastGenOutput>RazorTemplate.cs</LastGenOutput>
</None>
</ItemGroup>
This is not the answer as I misunderstood the question so the answer is actually what #SushiHangover put above...
To get the Xamarin Razor pages the project has to be a WebViewApp and then you can add New PreProcessed Razor View to the project.
From the Xamarin Blog on the subject.
See our Building Hybrid apps with Xamarin documentation to get started building Razor Hybrid applications. Simply use Xamarin Studio to create a new WebView Application for iOS or Android, or add a WebView to any existing iOS or Android layout, and use Add New…Preprocessed Razor Template to incorporate Razor-powered web views into your Xamarin apps. For a slightly more complex sample of a data-driven Razor hybrid app, check out our RazorTodo app.
The documentation can be found here
More detail here as this details how to add the Razor view to each project.
For Visual Studio 2022 I refer you to this answer that worked for me:
https://stackoverflow.com/a/41116061/812013
When I entered 'RazorTemplatePreprocessor' for the file's custom tool as the answer suggests, I initially didn't notice that Visual Studio opens a dialog asking you to install a ASP .NET extension first. Once I installed this extension, I was then getting a cs file generating from a cshtml file.

Include Content in csproj with a wildcard

First the problem I'm trying to solve. I work on a large team and we are constantly having merge conflicts on our web.csproj file. The solution I was trying to implement was including content files using a wild card.
I'm trying to include all files in a directory in my web.csproj file like so.
<Content Include="Areas\Public\Client\**\*.js" />
<Content Include="Areas\Public\Client\**\*.html" />
<Content Include="Areas\Public\Client\**\*css" />
This works great at first glance. I can see all files matching these patterns in visual studio. The problem is, if someone deletes a files from Visual Studio, the IDE then enumerates ALL files in the csproj file and removes my wildcard lines above.
Have any of you solved this problem or have any suggestions?
Thanks!
This is not possible in VS 2015 without loosing the functionality to add/rename/move/etc. items and keep the wildcards.
The handling of globbing in project files is only impelmented by the new CPS-based project system in VS 2017, which is not (yet) used for "classic" .NET / ASP.NET projects.
There are ways to add the Content items during the build using globbing patterns (using custom targets) but these will not show up in the solution explorer (which is okay for auto-generated files)

Automatically Include generated file in Visual Studio Project

I'm writing an application that will enable the creation of SQL files within visual studio. The user will enter commands via the Package Manager console which will generate sql scrips and deposit them in a specific directory within the Visual Studio Project.
The problem I have is that, when the files are generated, they are present on the file system, but not in Visual Studio. This is expected of course, as I need to then go and actively include the files within Solution explorer, but this isn't what I want. I want the files to "Magically" appear in solution explorer immediately after they're generated.
I've seen various solutions to similar problems mostly featuring amendments to the .csproj file such as this
<Compile Include="Sql\**\*.sql" />
but this isn't what i'm looking for. What i'm after is similar to how, for example, Entity Framework or MvcScaffolding work, where files / folders just magically drop into the project when commands run in PMC. I'm aware this runs off T4 templating, but that seems like too complex a solution for a simple issue like this.
I should qualify that there's no voodoo going on in the creation of the files, just plain old File.Create() stuff.
I'm open to any suggestions.
Thanks.
Check out this answer for a solution that worked for me. I have the same use-case where code outputs flat files and I need to include this output in the project.
At the end of your .csproj file add the following:
<Target Name="BeforeBuild">
<ItemGroup>
<Content Include="Sql\**\*.sql" />
</ItemGroup>
</Target>
IMHO, T4 is the way to go. You don't want to be bothering with older technologies for what you are trying to do.
Having said that, I wonder why is it required for the files to be added to the solution explorer? is it for source control purposes? (usually you don't want to source control auto generated files, you want to source control the original model).
Note that you could always click the 'show all' button and the files will appear in the solution explorer, without actually being a part of the solution.

Using an external tool for C# builds in Visual Studio

When using Visual Stdio 2008, you can make a C++ project build with an internal tool rather than having the IDE invoke MSVC directly. This improves the consistency of builds across platforms if a cross-platform build system is used.
However, I cannot figure out how to do the same as a C# project. It would be possible to simply register it as a native project with C# sources, however, you lose some of the advantages gained through having a C# project. More importantly, it will mean that allowing a project to build both directly and with an external tool (which is sadly necessary) will require two separate projects, rather than merely creating an alternate build configuration to invoke the external tool.
Does anyone know if it's possible to prevent Visual Studio from invoking csc by itself and instead call an external tool?
EDIT: Apparently there has some misunderstanding. The goal here is not to compile anything outside of Visual Studio. Instead, it's to allow Visual Studio to serve as the IDE but not the build system. There is already a (Scons-based) build system capable of compiling the C# and C++ sources, and Visual Studio has been configured to invoke Scons for compilation of C++ projects. I'm trying to configure it so that when you hit the 'Build' button, it will invoke Scons for the C# projects as well as the C++ ones.
Edit: Your question is still answered using MSBuild(if you are simply looking to compile outside the IDE). The IDE(Visual Studios) is simply a "fancy" way of constructing the build files that are built by MSBuild. Visual Studios isn't building the files, it simply is invoking MSBuild which ships with the .NET Framework 2.0 and up which compiles your code based on the project file that you create. If Scons can read and process an MSBuild file then I'm sure you can invoke it to build your project. But considering the fact that C# is a Microsoft language, I think you will be hard-pressed to find a value-add in not using MSBuild since I'd assume both the language and build tool are very tuned to work together. - End Edit
You can use MSBuild to compile your C# project. If you open your .csproj file in a text editor you will see that it is a MSBuild file. If you want to write some C# outside of the IDE you can construct a build file using the .csproj file as a starting point and invoke MSBuild to compile your apps. The IDE is just a way of abstracting the editing of the MSBuild file away for you.
If you are really industrious you can create a set of custom tasks to do things in your custom build process like move files around and versioning. MSBuild Community Tasks are a great example of using custom code to do task for you during MSBuild.
Given all the other answers, what MSBuild does when either VS or MSBuild perform a build can be found in the Targets files that ship with .Net. These can be be found in the FrameWork directory on your system. In my case:
C:\Windows\Microsoft.NET\Framework64\v3.5
Contains Microsoft.Common.targets among others. This file contains the following snippit:
<!--
============================================================
Build
The main build entry point.
============================================================
-->
<PropertyGroup>
<BuildDependsOn>
BeforeBuild;
CoreBuild;
AfterBuild
</BuildDependsOn>
</PropertyGroup>
<Target
Name="Build"
Condition=" '$(_InvalidConfigurationWarning)' != 'true' "
DependsOnTargets="$(BuildDependsOn)"
Outputs="$(TargetPath)"/>
This means that redifining this Target you can make MSBuild an VS do anything you want. The top of the mentioned file contains an important messagge:
Microsoft.Common.targets
WARNING: DO NOT MODIFY this file unless you are knowledgeable about MSBuild and have
created a backup copy. Incorrect changes to this file will make it
impossible to load or build your projects from the command-line or the IDE.
This file defines the steps in the standard build process for .NET projects. It
contains all the steps that are common among the different .NET languages, such as
Visual Basic, C#, and Visual J#.
My suggestion would be to read all you can about MSBuild and it's build file syntax and try redifining the Build target in your project(s). My impression is that after reading up on MSBuild you'll probably find an easier way to meet your requierements. You can find an example of redifining a Target like this in one of the answers of this so question .
Edit:
How to redefine a target?
Redefining is essentially defining the same target 'after' it has been defined. So for instance in your .*proj file(s) define a Build Task after the <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> line that imports all targets needed to in this case build a C# project. An example could be
<Target
Name="Build"
Condition=" '$(_InvalidConfigurationWarning)' != 'true' "
DependsOnTargets="BeforeBuild"
Outputs="$(TargetPath)">
<Exec Command="nmake" />
</Target>
I found a question in the same direction here, where it is suggested to edit the registry. I am pretty sure there is no other way to change the compiler used by Visual Studio because there is no trace of csc.exe in any solution, config, csproj file or whatsoever, nor in the Visual Studio 9.0 folder / subfolders within the Program Files dir.
Registry locations can be found in:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\74ACAA9F1F0087E4882A06A5E18D7D32
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\9055DA7481CC1024CB23A6109FD8FC9B
but those keys may differ dependng on your installation. Conclusion: changing the compiler used by VS seems next to impossible.
Addition: The following MSDN article deals with the same question for an custom C++ compiler, and Ed Dore's answer seems to confirm my theory that there's no way to choose an custom compiler for use within VS.
Under 'Tools' > 'External Tools' you should be able to define an outside tool to do activities for you. The Command should be the path to the executible for your external tool.
Hope this helps some.
You don't have to maintain different project files to build using an external tool. MSBuild is designed to build using the same project files that Visual Studio uses.
Here's an article that describes it.
Customize Your Builds in Visual Studio Using the Standalone MSBuild Tool
It's for VS2005, but should apply to VS2008 as well.
Looking through the answers, it seems clear to me that integrating scons into Visual Studio in a way that is compatible with the debugger and so on is not going to happen...
An option you might to consider, and I understand you don't want to change build systems, but bear with me, is to use a meta-build system, ie 'cmake'. http://www.cmake.org/
Cmake doeesn't actually build the project. What it does is to create build files for you, that you can use to build the project, and on Windows, the build files it creates for you are: Visual Studio project files. You can simply load those directly into your IDE, and compile, and use normally!
CMake is I feel very easy to use, and provides a high level of transparence and maintainability.
The exact same CMakeLists.txt files on linux will causes linux makefiles to be generated.
On mingw, they can generate mingw makefiles.
There are numerous generators available within cmake. The list is here:
http://www.cmake.org/cmake/help/cmake-2-8-docs.html#section_Generators
http://springrts.com is a huge opensource rts game that used to use scons as its cross-platform build system and now uses cmake.
I understand that you don't really want to have to change build systems, so it is a medium to long term solution.
Cmake is in any case one more option, to add to those of using a custom build tool, or using msbuild, or running the scons build from the commandline by hand.
Edit your project file and update the CscToolPath keys to point to the directory containing your tool and add CscToolExe keys that holds the name of the directory:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|.NET 3.5' ">
.
.
.
<CscToolPath>path\to\custom\tool\directory</CscToolPath>
<CscToolExe>exe name</CscToolExe>
.
.
.
</PropertyGroup>
I have not tested this, and the CscToolExe key may cause problems, in which case I would simply rename the external tool executable to "csc.exe".
You can build your solution from the command line like this:
C:\WINDOWS\Microsoft.NET\Framework\v3.5>msbuild.exe "C:\path\Your Solution.sln"

Categories

Resources