How to add a SqlMetal build step in VS2008? - visual-studio

I've created a one-line batch file to run SqlMetal to regenerate a LINQ to SQL DataContext for my database; this works great.
Refresh_DataContext.bat:
"C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\sqlmetal.exe"
/server:.\sqlexpress /database:MyDatabaseName
/code:"%~dp0\DataContext.vb" /context:DataContext
/views /functions /sprocs /pluralize
So far I can run this by opening the enclosing folder in Explorer and running the batch file, and I've also added it to the Tools menu (Tools/External Tools.../Add).
Now I'd like to run this batch file whenever I build (in certain configurations). How do I do this?

What you seem to be looking for can be found under "Pre-build event command line" under your project's properties. Click the "Build Events" tab, and you'll have a little space to enter in a command line which does what you want.

Doh! Of course. Web Application projects have pre- and post- build events; Web Site projects don't.

Related

How to execute custom file specific command / task in Visual Studio?

I would like to be able to define custom commands/task/macro for a VisualStudio solution. Then I would like to execute that command for a file that is selected in the Solution Explorer.
There are several possibilities to execute the command that would be fine for me:
a) Right click on the file in the Solution Explorer and choose the command from the context menu (my favorite)
b) Select a file in the Solution Explorer. Then click on a button on a tool bar. The command would then somehow retrieve the selected file from the Solution Explorer.
c) Select a file in the Solution Explorer. Then start a task from the Task Runner Explorer. The executed task would somehow retrive the selected file from the 'Solution Explorer'
I tried to use the VisualStudio extension VsCommandBuddy. However, it does not support file specific commands, see
https://github.com/PaulHuizer/VsCommandBuddy/issues/21
I also tried to use a Grunt or Gulp task that can be started from the Task Runner Explorer. However, I don't know how I can pass/access the file that is currently selected in the Solution Explorer.
https://blogs.msdn.microsoft.com/webdev/2016/01/06/task-runners-in-visual-studio-2015/
=> Is there a VisualStudio extension that easily allows to define custom commands for files?
=> How can I pass/access the file that is selected in the SolutionExplorer in script files (e.g. Gulp, Grunt, Webpack)?
=> Any other comfortable work flow that you would recommend?
It would be possible to write my own VisualStudio extension. But I guess that someone else already knows a solution for this.
1. Create an external tool command:
Tools=>External Tools=>Add
Use cmd.exe as Command
Use /c as first entry for `Arguments' and then specify the command line command you want to execute, e.g. grunt
Use the available variables, e.g. $(SolutionDir), $(ItemPath) (=file path), ... to customize your external command
2. Add external command as entry to the context menu of the solution explorer
Tools=>Customize=>Commands
Select Context menu: Project and Solution Context Menu | Item
Add Command...=>Tools=>External Command 1
Run command
Use the new context menu entry for items in the solution explorer
Stop command
In order to cancel/stop an external command, you can also use the context menu of the solution explorer. If the original name of the command is "Test with Karma", the title will be modified to "(Stop) Test with Karma" as long as the command is running. =>Select that entry to stop the command.
Export settings
Unfortunately those settings can not be stored with the Solution (?). It is however possible to export those settings. Then a colleague can import them:
Tools=> Import and export Settings...=>Next
General Settings=> External Tools List and
General Settings=> Menu and Command Bar Customizations
You can use my Visual Commander extension to define a custom command/macro. On how to get the currently selected file in Solution Explorer see In a VS 2015 extension, how can I get the selected object in the Solution Explorer?

How to configure post-build events for setup/deployment projects in Visual Studio

My solution has two projects. One is my actual project and another one is setup project.
My actual project has one report folder where I store my all ssrs report. I have one folder in the setup project called "SSRS_Repor". Now I want that when I will do a batch build then setup for my project regenerate, and then I want to copy all files from the report folder of my actual project to SSRS_Repor in my setup project.
If I can do this kind of automation of copying files from one location to another folder of my setup project, then I could be get rid of manual copying of rdls files. I heard this is possible by setup/deployment projects. I searched Google for this for details step-by-step instruction, but I didn't get any good links. So please guide me how can I do it.
I posted it to another forum too, and some one told me below this:
Open or create a setup/deployment project in Visual Studio 2005
Press F4 to display the Properties window
Click on the name of your setup/deployment project in the Solution Explorer
Click on the PostBuildEvent item in the Properties window to cause a button labeled "..." to appear
Click on the "..." button to display the Post-build Event Command Line dialog
Add a command line of your choice in the Post-build event command line text box
Build your project in Visual Studio and verify that the post-build event is executed after the main MSI build
So it is OK, but what do I need to write for copying files from one location to another location? That is not clear to me. So now this is most important for me what to write for copying file during setup generation.
I got another clue like below one. A script for setup Pre/Post Build Event, but not aware properly. I got a sample like
copy /Y "$(TargetDir)$(ProjectName).dll" "$(SolutionDir)lib\$(ProjectName).dll"
The above statement or line is not clear to me. What do I need to write in my case? I need a step-by-step guide.
Here is the screenshot of my project structure
To answer your question simply:
The commands that you input in the build events (be it pre or post) are the same as you would enter in a command line box.
In your example:
copy /Y "$(TargetDir)$(ProjectName).dll" "$(SolutionDir)lib\$(ProjectName).dll"
copy is the actual DOS copy command.
/Y is a regular switch that prevents confirmation prompts.
"$(TargetDir)$(ProjectName).dll" is the source file to copy.
"$(SolutionDir)lib\$(ProjectName).dll" is the destination where to copy the file.
You can refer here to have additional information on batch file commands: Batch command list
The $({Identifier}) are macros you can use in Visual Studio Pre/Post Build event designer.
You can refer to the MSDN online help for more details on macros: MSDN Macros List
The line provided to you would not do what you want. It's usually used to copy DLL files to a library folder used by some other projects or solution.
The solution you found to create a new build event is correct.
All you have left to do is write down the command that will actually copy the files.
It would look something like this:
XCOPY "$(SolutionDir)TestProject\Reports\*.*" "$(SolutionDir)TestSetup1\SSRS_Repor" /Q /E /I
/Q : Quiet | Don't display files being copied
/E : Recursive (copy subfolder structure and files)
/I : Consider that destination is a folder if it does not already exist (will create a new folder if required)

Sql Project Publish replaces Deploy - how to supress versioning and hook into tfs build

We're using the Sql Server 2012 SSDT which removed the deploy option in Visual Studio for the database projects (now sql projects). We'd like to automate the Publish step as we had for deploy, but it's not clear how to do this. so thA couple of questions:
I've added the .publish.xml to the project (after the first manual publish, checking add to project). Even after that, and setting it to the default, when I double click it, it builds, but always pops up settings window, where I need to click the "Publish" button to continue. Is there a setting that would skip this prompt and use the current values?
It seems that each publish generates a version of the sql output. How can I suppress this- i.e. overwrite the base file each time?
And lastly, any pointers for updating the build to use the new project type and publish command for the automated builds would be appreciated.
How to restore the Deploy option: (Visual Studio 2010/2012 only -- this is no longer supported in Visual Studio 2013)
The Deploy option is still present but for some reason it's not available in the menus. (Cursed Visual Studio team!) I've worked around this by adding the Deploy option to one of the toolbars as follows:
Click the arrow on the right-hand side of a toolbar.
Click "Add or Remove Buttons", then Customize.
In the Customize dialog click Add Command.
Select the "Build" category, then select the "Deploy Selection" command.
After saving your selection the "Deploy [project name]" option will appear on the toolbar. You'll need to select your project in Solution Explorer for the button to become enabled.
Note that the deployment settings are different than the publish settings. The deployment settings are configured in the project's properties on the Debug tab.
To answer your questions about the Publish option:
1) How to use a specific publish file by default and avoid the annoying prompt
I don't think there's a way around this.
2) How to publish the entire database, not just the changes
Open your .publish.xml file in a text editor and add <AlwaysCreateNewDatabase>true</AlwaysCreateNewDatabase>.
For example:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<TargetDatabaseName>MyDatabase</TargetDatabaseName>
<DeployScriptFileName>MyDatabaseProject.sql</DeployScriptFileName>
<TargetConnectionString>Data Source=localhost\SQL2012;Integrated Security=True;Pooling=False</TargetConnectionString>
<PublishDependentProjects>False</PublishDependentProjects>
<ProfileVersionNumber>1</ProfileVersionNumber>
<AlwaysCreateNewDatabase>true</AlwaysCreateNewDatabase>
</PropertyGroup>
</Project>
3) Command-line syntax for automated builds
First build your project with msbuild as you normally would so that the .dacpac file is created in the bin.
Then use sqlpackage.exe to publish using your .publish.xml file:
C:\Program Files\Microsoft Visual Studio 10.0\Microsoft SQL Server Data Tools\sqlpackage.exe /Action:Publish /SourceFile:C:\[path to my project]\bin\Debug\MyDatabaseProject.dacpac /Profile:C:\[path to my project]\MyDatabaseProject.publish.xml
Note that the path to sqlpackage.exe may be different.
A bit late to the party, I admit, but maybe this will help others who stumble across this discussion. My company is presently moving to VS2012 and we have all three of the same issues as Keith. I have found workarounds for #1 and #2.
For #1, I use AutoHotKey to monitor for the existence of the publish window, and automatically click the "Create Script" button. You could of course have the script automatically click the "Publish" button instead. In this example if the publish profile is not "XYZ" (I always prefer manual intervention for production server deployments) then go ahead and send an Alt+G to generate the script.
#Persistent
SetTimer, ClosePopups, 5000
return
ClosePopups:
if WinExist("Publish Database ")
{
WinActivate, Publish Database
WinGetTitle, TitleText, A
If not TitleText = "Publish Database XYZ.publish.xml" {
Send, !G
}
}
return
For #2, every time we publish it increments the filename with a number suffix and we end up with lots of files in our deployment folder. I just used pre-build events to clear out the .sql and .txt files before the build:
if exist "$(ProjectDir)$(OutputPath)*.publish.sql" del $(ProjectDir)$(OutputPath)*.publish.sql
if exist "$(ProjectDir)$(OutputPath)*.txt" del $(ProjectDir)$(OutputPath)*.txt
The best way I have found to automate the deployment of SSDT database projects is to use msbuild. Originally we were using VSTSDB and used msbuild against the *.dbproj file. As it turned out the arguments for deploying sqlproj files is exactly the same.
Because the old argument list works for us, I didnt swap to using the public.xml file style. There quite a bit of documentation for the vsdbcmd.exe and msbuild against dbproj. I would use that as reference.
Here's argument list, and execution output as we define it for FinalBuilder execution
[ MSBuild Project [ C:\xx\xxx\xx\xx\MyProject.sqlproj ] ]
Configuration : Release
OutDir : C:\Builds\1\xxxxx\builddefname\Binaries\Release\
DeployToDatabase : True
TargetDatabase : ExistingDatabaseName
TargetConnectionString : Data source=.;Integrated Security=SSPI;**
Build started 3/23/2012 2:17:08 PM.
Deployment script generated to:
C:\Builds\1\xxxx\builddefname_FB\Binaries\Release\MyProject.sql
Dropping FK_at_lusys_assetCategory_at_lusys_image...
Creating FK_dcb28374eeabe8e715038984419...
Creating FK_d82897e4acd966d4b136c242cef...
Checking existing data against newly created constraints
Update complete.
Done Building Project "C:\xxx\xxxxxxx\xxxxxxxxx\MyProject.sqlproj" (Deploy target(s)).
Build succeeded.
0 Warning(s)
0 Error(s)
and putting together the msbuild command line looks like this:
msbuild XXX.sqlproj /target:Deploy /p:Configuration=xxx;OutDir=xxx;DeployToDatabase=True;TargetDatabase=xxxx;TargetConnectionString="xxxxx";AlwaysCreateNewDatabase=True

Visual studio extension to redirect output files to specified folder

Usually, visual studio puts output files to bin/debug or bin/release.
When solution contains a large number of projects its not easy to modify each project output manually.
Also edits in csproj files no desirable, because some of them is shared between solutions..
My questions: Is anybody knows a tool, which can quickly configure output path ?
UPDATE: my problem solved by TFS Build
Presumably you have at least one project in each solution that is unique to that solution. In the Post-Build event of that, copy the contents of each project's output to the required location.
We often to this using a batch file. It's crude but effective. In our project that's unique to the solution we create a Release.bat file. This contains a number of file copies to copy all of the required components from the various output directories of the other projects. You can then just run the batch file in the post build event. We usually copy everything to a "Latest Release" fodler when the solution is built. If this becomes a proper release we will rename the Latest Release folder to the actual release number.
If you have multiple build configurations, or even just use the Debug and Release configurations, you can use an If statement in the Post-Build event to decide which batch file to run. So you could create a Debug.bat, Release.bat etc which do what you need. It can be tedious to set them up and get them working correctly at first, but they are very useful once fully implemented.
Customize your project using the msbuild properties which you can do if you follow these steps:
Go to the solution explorer and unload one project by right clicking on it and select Unload Project.
Then right click again on the unloaded project and select Edit Project. This will open the XML definition of your project, and you will have intellisense for the layout which will help you perform the next steps.
In the visual studio editor find the first PropertyGroup tag and add these lines near or at the end of the closing PropertyGroup tag:
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<BuildDirectory Condition="$(BuildDirectory) =='' or $(BuildDirectory) == '*Undefined*'">$(SolutionDir)\build\</BuildDirectory>
The above SolutionDir is defined in msbuild properties which you can obtain using this answer: msbuild script using solution information and also check out the well known msbuild properties here
The next step is to find the OutputPath tag for each configuration and edit it like this:
<OutputPath>$(BuildDirectory)\x86\AutomatedDebug\</OutputPath>
The example above assumes you have a configuration named AutomatedDebug with destination platform x86.
The output will be
x:\projects\whereever-your-solution-is\build\x86\AutomatedDebug\
Repeat for each project.
To unload more than one project, collapse all projects in the solution explorer and shift click or ctrl click to select all or some projects, then right click on the selected group to unload, unfortunately you cannot do this for editing, at least in visual studio 2010.
I am the first to admit that this is somewhat cumbersome to do for existing projects, but you could easily create a visual studio project template that has these settings changed so that new projects will use a more convenient default output directory.
You cannot edit the output directory directly in visual studio because the project properties editor escapes any $() enclosed text.
Also you could only modify the OutputPath using the name of a system environment variable enclosed in $(). This last option is to enable a global output directory.
If you build any single project modified in this way using msbuild directly in the commandline the output directory will be created one directory above from where you ran msbuild
..\build\x86\AutomatedDebug
If you are in a team, you should warn them not to edit the output directory directly by hand, as this action will overwrite any customization.
Hope this info is useful.
Greetings.

Is It possible to run stylecop on a single file in VS2010?

I've tried looking for command line options etc. I'm trying to lower the time I have to wait to see if a build fails due to stylecop.
This option does not appear in any context menus or the tools menu: Is it possible to run StyleCop on just 1 file in a project?
The following context menus should contain commands for running StyleCop:
At solution item in Solution Explorer (rescans the entire solution)
At project item in Solution Explorer (rescans a project)
At any single .cs file in Solution Explorer (scans single file)
Inside text editor of any single .cs file (scans single file)
By the way, which StyleCop version do you use?
Have you considered any issues about wrong installation?

Resources