Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Paket global and local tools #3737

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Paket.Bootstrapper/WindowsProcessArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Paket.Bootstrapper
{
static class WindowsProcessArguments
public static class WindowsProcessArguments
{
static void AddBackslashes(StringBuilder builder, int backslashes, bool beforeQuote)
{
Expand Down
11 changes: 11 additions & 0 deletions src/Paket.CmdLineHelpers/Paket.CmdLineHelpers.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\Paket.Bootstrapper\WindowsProcessArguments.cs" />
</ItemGroup>

</Project>
Empty file.
35 changes: 32 additions & 3 deletions src/Paket/Paket.fsproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net461;netcoreapp2.1</TargetFrameworks>
<TargetFrameworks>netcoreapp2.1;net461</TargetFrameworks>
<Version>5.239.0-beta-0001</Version>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<PackageId>Paket</PackageId>
<AssemblyName>paket</AssemblyName>
Expand All @@ -11,10 +12,14 @@
<PackageIconUrl>https://raw.githubusercontent.com/fsprojects/Paket/master/docs/files/img/logo.png</PackageIconUrl>
<PackageTags>nuget;bundler;F#</PackageTags>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.1' ">
<DefineConstants>PAKET_GLOBAL_LOCAL;$(DefineConstants)</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(PackAsTool)' == 'true' ">
<!-- .net tools support netcoreapp only -->
<TargetFrameworks></TargetFrameworks>
<TargetFramework>netcoreapp2.1</TargetFramework>
<DefineConstants>PAKET_GLOBAL_LOCAL;$(DefineConstants)</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.fs" />
Expand All @@ -25,14 +30,38 @@
</ItemGroup>
<ItemGroup Condition=" '$(PackAsTool)' == 'true' ">
<!-- include the merged paket.exe inside the package, in tools dir -->
<Content Include="..\..\bin\merged\paket.exe">
<!-- <Content Include="..\..\bin\merged\paket.exe">
<Pack>true</Pack>
<PackagePath>tools</PackagePath>
<Visible>true</Visible>
</Content>
</Content> -->
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Paket.Core\Paket.Core.fsproj" />
<ProjectReference Include="..\Paket.CmdLineHelpers\Paket.CmdLineHelpers.csproj" Condition=" '$(TargetFramework)' == 'netcoreapp2.1' " />
</ItemGroup>

<Target Name="Install">
<Exec Command='dotnet pack /p:PackAsTool=true' />

<!-- reinstall as global -->
<Exec Command='dotnet tool uninstall -g Paket'
IgnoreExitCode="true"
/>
<Exec Command='dotnet tool install -g Paket --version $(Version) --add-source "$(MSBuildThisFileDirectory)\bin\Debug"' />

<!-- reinstall as local -->
<Exec Command='dotnet tool uninstall --local Paket'
WorkingDirectory="C:\temp_p\a"
IgnoreExitCode="true"
/>
<RemoveDir Directories="C:\Users\e0s01ao\.nuget\packages\paket\$(Version)"
/>
<Exec Command='dotnet tool install --local Paket --version $(Version) --add-source "$(MSBuildThisFileDirectory)\bin\Debug"'
WorkingDirectory="C:\temp_p\a"
/>

</Target>

<Import Project="..\..\.paket\Paket.Restore.targets" />
</Project>
177 changes: 174 additions & 3 deletions src/Paket/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ let processWithValidationEx printUsage silent validateF commandF result =
traceError (" " + String.Join(" ",Environment.GetCommandLineArgs()))
printUsage result

Environment.ExitCode <- 1
1
else
try
commandF result
0
finally
sw.Stop()
if not silent then
Expand Down Expand Up @@ -866,6 +867,97 @@ let handleCommand silent command =
| Version
| Log_File _ -> failwithf "internal error: this code should never be reached."

#if PAKET_GLOBAL_LOCAL

(*
#load @"..\Paket.Core\Common\Domain.fs"
#load @"..\Paket.Core\Common\Logging.fs"
#load @"..\Paket.Core\Common\Constants.fs"
open System
open System.IO
open Paket
*)

let rec findRootInHierarchyFrom (dir: DirectoryInfo) =
if not (dir.Exists) then
None
else
let dotPaketDir =
Path.Combine(dir.FullName, Constants.PaketFolderName)
|> DirectoryInfo
if dotPaketDir.Exists then
Some dir
else
match dir.Parent with
| null -> None
| root -> findRootInHierarchyFrom root

let findRootInHierarchy () =
(Directory.GetCurrentDirectory() |> DirectoryInfo)
|> findRootInHierarchyFrom

let runIt exeName exeArgs =
use p = new Process()

// let argString : string = Paket.Bootstrapper.WindowsProcessArguments.ToString(exeArgs)
// let psi = ProcessStartInfo(FileName = exeName, Arguments = argString, UseShellExecute = false)
// printfn "Running: %s %A" exeName (psi.Arguments)

let psi = ProcessStartInfo(FileName = exeName, Arguments = "", UseShellExecute = false)
for arg in exeArgs do
psi.ArgumentList.Add(arg)
printfn "Running: %s %A" exeName (psi.ArgumentList |> Seq.toList)

p.StartInfo <- psi

p.Start() |> ignore
p.WaitForExit()
p.ExitCode


[<RequireQualifiedAccess>]
type GlobalCommand =
// global options
| [<AltCommandLine("-s");Inherit>] Silent
| [<AltCommandLine("-v");Inherit>] Verbose
// subcommands
| [<CustomCommandLine("init")>] Init of ParseResults<InitArgs>
with
interface IArgParserTemplate with
member this.Usage =
match this with
| Init _ -> "create an empty paket.dependencies file in the current working directory"
| Silent -> "suppress console output"
| Verbose -> "print detailed information to the console"

let globalCommandParser = ArgumentParser.Create<GlobalCommand>(programName = "paket", errorHandler = new ProcessExiter(), checkStructure = false)

let dotnetToolManifestPath (dir: DirectoryInfo) =
Path.Combine(dir.FullName, ".config", "dotnet-tools.json")
|> FileInfo

open System.Reflection

let isToolLocal () =
let x = typeof<AddArgs>.Assembly.Location
// printfn "%s" x

// local is like
// C:\Users\e0s01ao\.nuget\packages\paket\5.239.0-beta-0001\tools\netcoreapp2.1\any\paket.dll

// global is like
// C:\Users\e0s01ao\.dotnet\tools\.store\paket\5.239.0-beta-0001\paket\5.239.0-beta-0001\tools\netcoreapp2.1\any\paket.dll

let isToolGlobal = x.Contains(".store")

// printfn "tg %O" isToolGlobal

let isToolLocal = not isToolGlobal

isToolLocal

#endif

let main() =
let waitDebuggerEnvVar = Environment.GetEnvironmentVariable ("PAKET_WAIT_DEBUGGER")
if waitDebuggerEnvVar = "1" then
Expand Down Expand Up @@ -932,12 +1024,91 @@ let main() =
| None -> null

handleCommand silent (results.GetSubCommand())
else
0
with
| exn when not (exn :? System.NullReferenceException) ->
Environment.ExitCode <- 1
traceErrorfn "Paket failed with"
if Environment.GetEnvironmentVariable "PAKET_DETAILED_ERRORS" = "true" then
printErrorExt true true true exn
else printError exn
1

#if PAKET_GLOBAL_LOCAL

let handleGlobalCommand silent command =
match command with
| GlobalCommand.Init r -> processCommand silent (init) r
// global options; list here in order to maintain compiler warnings
// in case of new subcommands added
| GlobalCommand.Verbose
| GlobalCommand.Silent -> failwithf "internal error: this code should never be reached."

main()
let mainGlobal () =
let waitDebuggerEnvVar = Environment.GetEnvironmentVariable ("PAKET_WAIT_DEBUGGER")
if waitDebuggerEnvVar = "1" then
waitForDebugger()

Logging.verboseWarnings <- Environment.GetEnvironmentVariable "PAKET_DETAILED_WARNINGS" = "true"
use consoleTrace = Logging.event.Publish |> Observable.subscribe Logging.traceToConsole

// TODO ^--- no need to duplicate these, move in entrypoint

try
let parser = ArgumentParser.Create<GlobalCommand>(programName = "paket",
helpTextMessage = sprintf "Paket version %s%sHelp was requested:" paketVersion Environment.NewLine,
errorHandler = new PaketExiter(),
checkStructure = false)

let results = parser.ParseCommandLine(raiseOnUsage = true)
let silent = results.Contains <@ GlobalCommand.Silent @>
tracePaketVersion silent

if results.Contains <@ GlobalCommand.Verbose @> then
Logging.verbose <- true
Logging.verboseWarnings <- true

handleGlobalCommand silent (results.GetSubCommand())

//TODO v---- no need to duplicate, move in entrypoint? or not
with
| exn when not (exn :? System.NullReferenceException) ->
traceErrorfn "Paket failed with"
if Environment.GetEnvironmentVariable "PAKET_DETAILED_ERRORS" = "true" then
printErrorExt true true true exn
else printError exn
1

#endif

[<EntryPoint>]
let theMain argv =
#if PAKET_GLOBAL_LOCAL
let isToolLocal = isToolLocal ()

if isToolLocal then
main ()
else
let paketRoot = findRootInHierarchy ()
match paketRoot with
| None ->
// act as global tool (subset of commands)
mainGlobal ()
| Some dir ->
let manifestToolPath = dotnetToolManifestPath dir
if manifestToolPath.Exists then
// paket as local tool => `dotnet paket`

//TODO check if paket is installed in manifest! otherwise tell to user!

runIt "dotnet" [| yield "paket"; yield! argv |]
else
// old paket => `.paket/paket`
let paketExePath =
Path.Combine(dir.FullName, Constants.PaketFolderName, Constants.PaketFileName)
|> Path.GetFullPath
runIt paketExePath argv

#else
main ()
#endif