Skip to content

Instantly share code, notes, and snippets.

@fxthomas
Last active December 14, 2020 09:19
Show Gist options
  • Save fxthomas/5c601e3e0c1a091bcf56aed0f2960cfa to your computer and use it in GitHub Desktop.
Save fxthomas/5c601e3e0c1a091bcf56aed0f2960cfa to your computer and use it in GitHub Desktop.
Example project for custom pip build integration and setup.py menu items in Visual Studio Python Tools
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>xxxx</ProjectGuid>
<ProjectHome>.</ProjectHome>
<StartupFile>setup.py</StartupFile>
<SearchPath>lib\</SearchPath>
<WorkingDirectory>.</WorkingDirectory>
<OutputPath>.</OutputPath>
<Name>PythonAPI</Name>
<RootNamespace>PythonAPI</RootNamespace>
<IsWindowsApplication>False</IsWindowsApplication>
<LaunchProvider>Standard Python launcher</LaunchProvider>
<EnableNativeCodeDebugging>False</EnableNativeCodeDebugging>
<Environment></Environment>
<InterpreterId>{xxx}</InterpreterId>
<InterpreterVersion>2.7</InterpreterVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugSymbols>true</DebugSymbols>
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugSymbols>true</DebugSymbols>
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
</PropertyGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<PtvsTargetsFile>$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets</PtvsTargetsFile>
</PropertyGroup>
<ItemGroup>
<Folder Include="lib\package\" />
<Folder Include="lib\package\subdir\" />
<Folder Include="lib\" />
</ItemGroup>
<ItemGroup>
<Compile Include="lib\package\subdir\file1.py" />
<Compile Include="lib\package\subdir\file2.py" />
<Compile Include="lib\package\subdir\__init__.py" />
<Compile Include="lib\package\__init__.py" />
<Compile Include="setup.py" />
</ItemGroup>
<ItemGroup>
<InterpreterReference Include="{xxxxxx}\3.5" />
<InterpreterReference Include="{xxxxxx}\2.7" />
</ItemGroup>
<ItemGroup>
<Content Include="README.txt" />
<Content Include="requirements.txt" />
<Content Include="setup.cfg" />
</ItemGroup>
<Import Condition="Exists($(PtvsTargetsFile))" Project="$(PtvsTargetsFile)" />
<Import Condition="!Exists($(PtvsTargetsFile))" Project="$(MSBuildToolsPath)\Microsoft.Common.targets" />
<!-- Uncomment the CoreCompile target to enable the Build command in
Visual Studio and specify your pre- and post-build commands in
the BeforeBuild and AfterBuild targets below. -->
<Target Name="CoreCompile" />
<Target Name="BeforeBuild">
</Target>
<PropertyGroup>
<!-- Python package configuration -->
<PackageName>package</PackageName>
<PackageVersion>1.0.0</PackageVersion>
<DistributionOutputDir>$(ProjectDir)dist\$(PackageName)-$(PackageVersion)</DistributionOutputDir>
<DistributionArchive>$(ProjectDir)dist\$(PackageName)-$(PackageVersion).zip</DistributionArchive>
<InstallerTitle>Example Package $(PackageVersion)</InstallerTitle>
<!-- Note: Change to "true" to allow building the output archive -->
<EnableBuildIntegration>false</EnableBuildIntegration>
</PropertyGroup>
<!-- See: https://github.com/moozzyk/MSBuild-Tasks (MIT license) -->
<UsingTask TaskName="Zip" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<InputFileNames ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
<OutputFileName ParameterType="System.String" Required="true" />
<OverwriteExistingFile ParameterType="System.Boolean" Required="false" />
</ParameterGroup>
<Task>
<Reference Include="System.IO.Compression" />
<Using Namespace="System.IO.Compression" />
<Code Type="Fragment" Language="cs"><![CDATA[
const int BufferSize = 64 * 1024;
var buffer = new byte[BufferSize];
var fileMode = OverwriteExistingFile ? FileMode.Create : FileMode.CreateNew;
using (var outputFileStream = new FileStream(OutputFileName, fileMode))
{
using (var archive = new ZipArchive(outputFileStream, ZipArchiveMode.Create))
{
foreach (var inputFileName in InputFileNames.Select(f => f.ItemSpec))
{
var archiveEntry = archive.CreateEntry(Path.GetFileName(inputFileName));
using (var fs = new FileStream(inputFileName, FileMode.Open))
{
using (var zipStream = archiveEntry.Open())
{
int bytesRead = -1;
while ((bytesRead = fs.Read(buffer, 0, BufferSize)) > 0)
{
zipStream.Write(buffer, 0, bytesRead);
}
}
}
}
}
}
]]></Code>
</Task>
</UsingTask>
<Target Name="AfterBuild" Inputs="@(Compile)" Outputs="$(ProjectDir)dist\release.zip" Condition="'$(EnableBuildIntegration)' == 'true'">
<RemoveDir Directories="$(DistributionOutputDir)">
</RemoveDir>
<Delete Files="$(DistributionArchive)" />
<RunPythonCommand Target="$(ProjectDir)setup.py" TargetType="script" Arguments="bdist_wininst --user-access-control=force --title &quot;$(InstallerTitle)&quot; --dist-dir=&quot;$(DistributionOutputDir)&quot;" WorkingDirectory="$(WorkingDirectory)" RequiredPackages="setuptools" ExecuteIn="console">
</RunPythonCommand>
<RunPythonCommand Target="$(ProjectDir)setup.py" TargetType="script" Arguments="bdist_wheel --dist-dir=&quot;$(DistributionOutputDir)&quot;" WorkingDirectory="$(WorkingDirectory)" RequiredPackages="wheel;setuptools" ExecuteIn="console">
</RunPythonCommand>
<ItemGroup>
<DistributionFiles Include="$(DistributionOutputDir)\*.*" />
<DistributionFiles Include="$(ProjectDir)README.txt" />
</ItemGroup>
<Zip InputFileNames="@(DistributionFiles)" OutputFileName="$(DistributionArchive)" />
</Target>
<PropertyGroup>
<PythonCommands>
PipInstallDevCommand;
BdistWinInstCommand;
BdistWheelCommand;
$(PythonCommands);
</PythonCommands>
</PropertyGroup>
<Target Name="PipInstallDevCommand" Label="Install package for development" Returns="@(Commands)">
<CreatePythonCommandItem Target="pip" TargetType="module" Arguments="install --editable $(ProjectDir)" WorkingDirectory="$(WorkingDirectory)" ExecuteIn="Repl:Install package for development">
<Output TaskParameter="Command" ItemName="Commands" />
</CreatePythonCommandItem>
</Target>
<Target Name="BdistWinInstCommand" Label="Generate Windows Installer" Returns="@(Commands)">
<CreatePythonCommandItem Target="$(ProjectDir)setup.py" TargetType="script" Arguments="bdist_wininst --user-access-control=force --title &quot;$(InstallerTitle)&quot; --dist-dir=&quot;$(DistributionOutputDir)&quot;" WorkingDirectory="$(WorkingDirectory)" RequiredPackages="setuptools" ExecuteIn="Repl:Generate Windows Installer">
<Output TaskParameter="Command" ItemName="Commands" />
</CreatePythonCommandItem>
</Target>
<Target Name="BdistWheelCommand" Label="Generate Wheel Package" Returns="@(Commands)">
<CreatePythonCommandItem Target="$(ProjectDir)setup.py" TargetType="script" Arguments="bdist_wheel --dist-dir=&quot;$(DistributionOutputDir)&quot;" WorkingDirectory="$(WorkingDirectory)" RequiredPackages="wheel;setuptools" ExecuteIn="Repl:Generate Wheel Package">
<Output TaskParameter="Command" ItemName="Commands" />
</CreatePythonCommandItem>
</Target>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment