Skip to content

Commit

Permalink
Merge pull request #125 from kaeedo/feature/ShowDifferentTempalteOptions
Browse files Browse the repository at this point in the history
Feature/show different tempalte options
  • Loading branch information
AngelMunoz committed Apr 24, 2023
2 parents 088de87 + 4888822 commit ab4aec4
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 109 deletions.
4 changes: 2 additions & 2 deletions sample/src/App.fsproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand All @@ -16,4 +16,4 @@
<Content Include="js-file.js" />
</ItemGroup>
<Import Project="..\..\.paket\Paket.Restore.targets" />
</Project>
</Project>
3 changes: 2 additions & 1 deletion src/Perla/CliMiddleware.fs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ module Middleware =
env.RunSetupTemplates(
{
fullRepositoryName =
$"{Default_Templates_Repository}:{Default_Templates_Repository_Branch}"
Some
$"{Default_Templates_Repository}:{Default_Templates_Repository_Branch}"
operation = RunTemplateOperation.Add
},
cancellationToken
Expand Down
1 change: 1 addition & 0 deletions src/Perla/CliMiddleware.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ module Middleware =
bool

val ToSCLMiddleware: middleware: PerlaCliMiddleware -> InvocationMiddleware

val previewCheck:
command: Command * directives: KeyValuePair<string, string seq> seq ->
Task<Result<unit, PerlaMdResult>>
Expand Down
17 changes: 4 additions & 13 deletions src/Perla/Commands.fs
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,13 @@ module PackageInputs =
let showAsNpm: HandlerInput<bool option> =
Input.OptionMaybe(
[ "--npm"; "--as-package-json"; "-j" ],
"Show the packages simlar to npm's package.json"
"Show the packages similar to npm's package.json"
)

[<RequireQualifiedAccess>]
module TemplateInputs =
let repositoryName: HandlerInput<string> =
Input.Argument(
let repositoryName: HandlerInput<string option> =
Input.ArgumentMaybe(
"templateRepositoryName",
"The User/repository name combination"
)
Expand Down Expand Up @@ -332,12 +332,6 @@ module ProjectInputs =
let projectName: HandlerInput<string> =
Input.Argument("name", "Name of the new project")

let templateName: HandlerInput<string option> =
Input.OptionMaybe(
[ "-tn"; "--template-name" ],
"repository/directory combination of the template name, or the full name in case of name conflicts username/repository/directory"
)

let byId: HandlerInput<string option> =
Input.OptionMaybe(
[ "-id"; "--group-id" ],
Expand Down Expand Up @@ -719,7 +713,7 @@ module Commands =
let buildArgs
(
ctx: InvocationContext,
name: string,
name: string option,
add: bool option,
update: bool option,
remove: bool option,
Expand Down Expand Up @@ -786,13 +780,11 @@ module Commands =
(
ctx: InvocationContext,
name: string,
template: string option,
byId: string option,
byShortName: string option
) : ProjectOptions * CancellationToken =
{
projectName = name
byTemplateName = template
byId = byId
byShortName = byShortName
},
Expand All @@ -807,7 +799,6 @@ module Commands =
inputs (
Input.Context(),
ProjectInputs.projectName,
ProjectInputs.templateName,
ProjectInputs.byId,
ProjectInputs.byShortName
)
Expand Down
3 changes: 1 addition & 2 deletions src/Perla/Commands.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module PackageInputs =

[<RequireQualifiedAccess>]
module TemplateInputs =
val repositoryName: HandlerInput<string>
val repositoryName: HandlerInput<string option>
val addTemplate: HandlerInput<bool option>
val updateTemplate: HandlerInput<bool option>
val removeTemplate: HandlerInput<bool option>
Expand All @@ -68,7 +68,6 @@ module TemplateInputs =
[<RequireQualifiedAccess>]
module ProjectInputs =
val projectName: HandlerInput<string>
val templateName: HandlerInput<string option>
val byId: HandlerInput<string option>
val byShortName: HandlerInput<string option>

Expand Down
22 changes: 17 additions & 5 deletions src/Perla/Configuration.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,29 @@ module internal ConfigExtraction =
module FromDecoders =
open Json.ConfigDecoders

val GetFable: FableConfig option * DecodedFableConfig option -> FableConfig option
val GetDevServer: DevServerConfig * DecodedDevServer option -> DevServerConfig option
val GetFable:
FableConfig option * DecodedFableConfig option -> FableConfig option

val GetDevServer:
DevServerConfig * DecodedDevServer option -> DevServerConfig option

val GetBuild: BuildConfig * DecodedBuild option -> BuildConfig option
val GetEsbuild: EsbuildConfig * DecodedEsbuild option -> EsbuildConfig option

val GetEsbuild:
EsbuildConfig * DecodedEsbuild option -> EsbuildConfig option

val GetTesting: TestConfig * DecodedTesting option -> TestConfig option

[<RequireQualifiedAccess>]
module FromFields =
val GetServerFields: DevServerConfig * DevServerField seq option -> DevServerField seq
val GetServerFields:
DevServerConfig * DevServerField seq option -> DevServerField seq

val GetMinify: RunConfiguration * DevServerField seq -> bool
val GetDevServerOptions: DevServerConfig * DevServerField seq -> DevServerConfig

val GetDevServerOptions:
DevServerConfig * DevServerField seq -> DevServerConfig

val GetTesting: TestConfig * TestingField seq option -> TestConfig

val FromEnv: config: PerlaConfig -> PerlaConfig
Expand Down
3 changes: 1 addition & 2 deletions src/Perla/FileSystem.fs
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,7 @@ module FileSystem =
let extract () =
use stream = new GZipInputStream(File.OpenRead path)

use archive =
TarArchive.CreateInputTarArchive(stream, Encoding.UTF8)
use archive = TarArchive.CreateInputTarArchive(stream, Encoding.UTF8)

path |> Path.GetDirectoryName |> archive.ExtractContents

Expand Down
4 changes: 3 additions & 1 deletion src/Perla/FileSystem.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ module FileSystem =
val TplRepositoryChildTemplates:
path: string<SystemPath> -> string<SystemPath> seq

val DotNetToolRestore: cancellationToken: CancellationToken -> Task<Result<unit, string>>
val DotNetToolRestore:
cancellationToken: CancellationToken -> Task<Result<unit, string>>

val CheckFableExists: cancellationToken: CancellationToken -> Task<bool>

[<Class>]
Expand Down
81 changes: 33 additions & 48 deletions src/Perla/Handlers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,12 @@ type RunTemplateOperation =
| List of ListFormat

type TemplateRepositoryOptions = {
fullRepositoryName: string
fullRepositoryName: string option
operation: RunTemplateOperation
}

type ProjectOptions = {
projectName: string
byTemplateName: string option
byId: string option
byShortName: string option
}
Expand Down Expand Up @@ -188,13 +187,7 @@ module Templates =
let table =
Table()
.AddColumns(
[|
"Name"
"Group"
"Belongs to"
"Parent Location"
"Last Update"
|]
[| "Name"; "perla new -t"; "perla new -id"; "Repository name" |]
)

for column in table.Columns do
Expand All @@ -204,51 +197,37 @@ module Templates =
let name: Rendering.IRenderable =
Markup($"[bold green]{template.name}[/]")

let group = Markup($"[bold yellow]{template.group}[/]")
let belongsTo = Markup($"[bold blue]{parent.ToFullNameWithBranch}[/]")

let lastUpdate =
parent.updatedAt
|> Option.ofNullable
|> Option.defaultValue parent.createdAt
|> (fun x -> $"[bold green]{x.ToShortDateString()}[/]")
|> Markup

let location =
TextPath(
UMX.untag parent.path,
LeafStyle = Style(Color.Green),
StemStyle = Style(Color.Yellow),
SeparatorStyle = Style(Color.Blue)
)
let shortname = Markup($"[bold yellow]{template.shortName}[/]")
let group = Markup($"[bold blue]{template.group}[/]")

table.AddRow([| name; group; belongsTo; location; lastUpdate |])
|> ignore
let repositoryName =
Markup($"[bold blue]{parent.ToFullNameWithBranch}[/]")

table.AddRow([| name; shortname; group; repositoryName |]) |> ignore

AnsiConsole.Write table
0
| ListFormat.TextOnly ->
let columns =
Columns([| "Name"; "Group"; "Belongs to"; "Parent Location" |])
let columns = Columns([| "Name"; "perla new -t"; "perla new -id" |])

AnsiConsole.Write columns

let rows = [|
for parent, template in results do
let name = Markup($"[bold green]{template.name}[/]")
let group = Markup($"[bold yellow]{template.group}[/]")
let shortname = Markup($"[bold yellow]{template.shortName}[/]")
let group = Markup($"[bold blue]{template.group}[/]")

let belongsTo = Markup($"[bold blue]{parent.ToFullNameWithBranch}[/]")

let location =
let repositoryName =
TextPath(
UMX.untag parent.path,
UMX.untag parent.ToFullNameWithBranch,
LeafStyle = Style(Color.Green),
StemStyle = Style(Color.Yellow),
SeparatorStyle = Style(Color.Blue)
)

Columns(name, group, belongsTo, location) :> Rendering.IRenderable
Columns(name, shortname, group, repositoryName)
:> Rendering.IRenderable
|]

rows |> Rows |> AnsiConsole.Write
Expand Down Expand Up @@ -591,14 +570,10 @@ module Handlers =
|> Option.map UMX.tag<TemplateGroup>
|> Option.map QuickAccessSearch.Group

let inline byTemplateName () =
options.byTemplateName |> Option.map QuickAccessSearch.Name

let queryParam =
options.byShortName
|> Option.map QuickAccessSearch.ShortName
|> Option.orElseWith byId
|> Option.orElseWith byTemplateName

let foundRepo = result {
let! query = queryParam |> Result.requireSome Templates.NoQueryParams
Expand Down Expand Up @@ -773,15 +748,25 @@ module Handlers =
}

let updateRepo () = task {
let template = template.Value
Logger.log $"Template {template.ToFullNameWithBranch} already exists."
match template with
| ValueSome template ->
Logger.log $"Template {template.ToFullNameWithBranch} already exists."

match!
Templates.AddOrUpdate(Templates.TemplateOperation.Update template)
with
| Ok() -> return 0
| Error err ->
Logger.log (err, escape = false)
return 1
| ValueNone ->
Logger.log "We were unable to parse the repository name."

Logger.log (
"please ensure that the repository name is in the format: [bold blue]username/repository:branch[/]",
escape = false
)

match!
Templates.AddOrUpdate(Templates.TemplateOperation.Update template)
with
| Ok() -> return 0
| Error err ->
Logger.log (err, escape = false)
return 1
}

Expand Down
11 changes: 5 additions & 6 deletions src/Perla/Handlers.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,12 @@ type RunTemplateOperation =
| List of ListFormat

type TemplateRepositoryOptions = {
fullRepositoryName: string
fullRepositoryName: string option
operation: RunTemplateOperation
}

type ProjectOptions = {
projectName: string
byTemplateName: string option
byId: string option
byShortName: string option
}
Expand All @@ -94,12 +93,12 @@ type DescribeOptions = {

[<Struct>]
type PathOperation =
| AddOrUpdate of addImport: string<BareImport> * addPath: string<ResolutionUrl>
| AddOrUpdate of
addImport: string<BareImport> *
addPath: string<ResolutionUrl>
| Remove of removeImport: string

type PathsOptions = {
operation: PathOperation
}
type PathsOptions = { operation: PathOperation }

module Handlers =

Expand Down
18 changes: 11 additions & 7 deletions src/Perla/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ open Spectre.Console.Rendering
open FSharp.UMX
open Perla.Units
open Perla.PackageManager.Types
open FsToolkit.ErrorHandling

module private ImportMap =

Expand Down Expand Up @@ -75,15 +76,18 @@ module Lib =
ValueSome(Provider.Unpkg, name, $"{version}{preview}")
| _ -> ValueNone

let parseFullRepositoryName (value: string) =
let parseFullRepositoryName (value: string option) = voption {
let! name = value
let regex = Regex(@"^([-_\w\d]+)\/([-_\w\d]+):?([\w\d-_]+)?$")

match value with
| ParseRegex regex [ username; repository; branch ] ->
ValueSome(username, repository, branch)
| ParseRegex regex [ username; repository ] ->
ValueSome(username, repository, "main")
| _ -> ValueNone
return!
match name with
| ParseRegex regex [ username; repository; branch ] ->
ValueSome(username, repository, branch)
| ParseRegex regex [ username; repository ] ->
ValueSome(username, repository, "main")
| _ -> ValueNone
}

let getTemplateAndChild (templateName: string) =
match
Expand Down
2 changes: 1 addition & 1 deletion src/Perla/Library.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Lib =
url: string -> (Provider * string * string) voption

val internal parseFullRepositoryName:
value: string -> (string * string * string) voption
value: string option -> (string * string * string) voption

val internal getTemplateAndChild:
templateName: string -> string option * string * string option
Expand Down
5 changes: 5 additions & 0 deletions src/Perla/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@
"commandName": "Project",
"commandLineArgs": "add @esm-bundle/chai -d",
"workingDirectory": "$(ProjectDir)/../../sample"
},
"Show templates in table": {
"commandName": "Project",
"commandLineArgs": "t foo -ls table",
"workingDirectory": "$(ProjectDir)/../../sample"
}
}
}
Loading

0 comments on commit ab4aec4

Please sign in to comment.