Skip to content

Commit

Permalink
main
Browse files Browse the repository at this point in the history
  • Loading branch information
sharpchen committed Dec 15, 2024
1 parent da5ed0c commit 49a0ea8
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 18 deletions.
13 changes: 13 additions & 0 deletions docs/document/Articles/docs/Make Nix Derivation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Make Nix Derivation

Making a nix derivation typically includes following steps.

1. **Choose a derivation function**: `pkgs` has some wrapper functions of `mkDerivation` for different frameworks. See:[Languages and Frameworks](https://nixos.org/manual/nixpkgs/stable/#chap-language-support)
- `mkDerivation` is a wrapper of `derivation` function.
2. **Find a source**: where the source code come from? github repo or tarball or any other available from url.
- If the source is precompiled, should set a appropriate value for `meta.sourceProvenance`. See: [Source provenance](https://ryantm.github.io/nixpkgs/stdenv/meta/#sec-meta-sourceProvenance)
3. **Choose a fetcher**: use a appropriate fetcher to download the source. See [Fetchers](https://nixos.org/manual/nixpkgs/unstable/#chap-pkgs-fetchers)
4. **Generate lock file**: if you'd build from source, this might require a lock file so nix can download dependencies during build.
- If the source is precompiled, this is probably not necessary.
- Lock file is typically required when using a wrapper function of `mkDerivation` for certain framework.
5.
13 changes: 13 additions & 0 deletions docs/document/PowerShell/docs/Language/Array.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,19 @@ Keyword operators has special functionalities on collections.
> [!NOTE]
> If current item for `-match` or `-notmatch` is not a string, Powershell evaluates it to string by certain strategy.
> [!TIP]
> Match and replace is case-insensitive by default. Use `-cmatch`, `-cnotmatch`, `-creplace`, `-csplit` for **case-sensitive** scenarios.
## Deconstruction

```ps1
$a, $b, $c = 1,2,3 # $a = 1, $b = 2, $c = 3
$a, $b = 1,2,3 # $a = 1, $b = 3 This might not be expected # [!code warning]
```

> [!WARNING]
> You should only use deconstruction when **you need the first and the last item returned** or **you're pretty sure there's only two element will be returned**.
## Multi-Dim Array

You'll have to create Multi-Dim array from .NET type constructor only.
Expand Down
19 changes: 19 additions & 0 deletions docs/document/PowerShell/docs/Language/Control Flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,25 @@ cd ..; gci -rec -file; echo hello

In PowerShell 7, `&&` and `||` were introduced to do the same command chaining as bash does.

## Null-Conditional Operator <Badge type="info" text="PowerShell 7+" />

This feature is basically the same as in `C#` except a bracing `{}` is required around variable because `?` is valid as part of variable name.

```ps1
$null.Bar() # exception raised # [!code error]
${null}?.Bar() # ok
$null[0] # exception raised here # [!code error]
${null}?[0] # ok
$null ?? 'I am not null'
$baz ??= 'I am new value'
```

## Ternary Operator <Badge type="info" text="PowerShell 7+" />

No need to explain, same as in `C#`.

## Pattern Matching

Expand Down
17 changes: 13 additions & 4 deletions docs/document/PowerShell/docs/Language/String.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Use `$` to interpolate:
```

> [!NOTE]
> `$()` is being called **SubExpression Operator** in Powershell.
> `$()` is being called **SubExpression Operator** in PowerShell.
## Verbatim String

Expand All @@ -36,12 +36,12 @@ Can contains new lines and
```

> [!NOTE]
> Verbatim string does not allow interpolation in Powershell which differs from `C#`.
> Verbatim string does not allow interpolation in PowerShell which differs from `C#`.
## Raw String

> [!NOTE]
> Raw string is being called **Here String** in Powershell.
> Raw string is being called **Here String** in PowerShell.
**Here String** typically allows string contents starting at the second line and truncates the last newline.

Expand Down Expand Up @@ -93,7 +93,7 @@ Use double `'` to escape `'` in a verbatim string.
## Arithmetic with Numerics

Powershell will try to convert the string on the right operand to the same type as left operand.
PowerShell will try to convert the string on the right operand to the same type as left operand.
Exception will be raised if conversion failed.

```ps1
Expand All @@ -111,6 +111,15 @@ Exception will be raised if conversion failed.

## Match & Replace

PowerShell has two kinds of matching strategy for strings.
- `-match` for regex matching.
- `-like` for wildcard matching.

While `-replace` only supports regex.

> [!TIP]
> Match and replace is case-insensitive by default. Use `-cmatch`, `-cnotmatch`, `-creplace`, `-csplit` for **case-sensitive** scenarios.
```ps1
'Janet is a girl' -match 'Jane' # True
'Janet is a girl' -replace '^Janet', 'Jane'
Expand Down
9 changes: 7 additions & 2 deletions docs/document/PowerShell/docs/Object Manipulation/ForEach.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ If you do want a `$null` return, use `Out-Null` to swallow the value.

```ps1
# doesn't make much sense though
(gci | foreach { $_.Name | Out-Null }) -eq $null # True
$null -eq (gci | foreach { $_.Name | Out-Null }) # True
```


> [!NOTE]
> One exception is `ForEach-Object` can execute method by name.
> You might expect delegate objects to be returned, but no, values of the method being executed will be returned.
>```ps1
>(1,2,3 | % GetType | select -first 1) -is [System.Type] # True
>```
4 changes: 3 additions & 1 deletion docs/document/PowerShell/docs/Object Manipulation/Select.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Select

## Overview
`Select-Object` serves for multiple purposes for object transformation and picking from collection.

The following is a overview:

- Picking composed object by one or more properties with `-Property`.
- select properties reversely by `-ExcludeProperty`.
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion docs/document/PowerShell/docs/Terminology.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Powershell has its own terminologies besides other shells like bash.
It's a shell integrated with .NET, it's not a independent platform.

- `cmdlet`: builtin utils inside powershell, implemented using other languages like `C#`.
- `cmdlet`: precompiled .NET assemblies work for PowerShell, implemented using other languages typically in `C#`.
- `function`: a command implemented using powershell language itself.
- `application`: external executable.

Expand Down
55 changes: 45 additions & 10 deletions docs/document/PowerShell/docs/Type System/Extended Type System.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,49 @@
# Extended Type System

Extended Type System(ETS) is for consistent experience with **some** `.NET` types when working with Powershell.
Extended Type System(ETS) is for consistent experience with **some** `.NET` types when working with PowerShell.

For this purpose, Powershell wraps the traget object as `PSObject` with two approaches:
There's two approaches that PowerShell did for implementing the ETS.

- Set the object to be wrapped as a meta object(similar to lua metatable)
- Add extra members to the containing `PSObject` itself.
- Intrinsic members for all objects.
- Dynamic manipulation to members of an object.
- Potentially wrap an object as `PSObject`.

## Intrinsic Members

There's two kinds of intrinsic members in PowerShell
- Object views: the mapping of a certain object.
- Property and methods: intrinsic properties and methods.

All objects in PowerShell have five **object views** members:

- `psobject`: a `MemberSet` containing reflection source of members.
```ps1
$foo = 'I am foo'
[object]::ReferenceEquals($foo, $foo.psobject.BaseObject) # True
```
- `psbase`: a `MemberSet` containing members of the object being wrapped.
```ps1
$foo = 'I am foo'
[object]::ReferenceEquals($foo.ToString, $foo.psbase.ToString) # True
```
- `psadapted`: a `MemberSet` containing adapted members added by ETS.
- `psextended`: a `MemberSet` containing extended members **added at runtime**.
- `pstypenames`: a `CodeProperty` equivalent to `psobject.TypeNames`. Returns a collection containing the type names of inheritance chain.
```ps1
$foo = 'I am foo'
[object]::ReferenceEquals($foo.psobject.TypeNames, $foo.pstypenames) # True
```

Intrinsic methods and properties are to mimic singular object and collection in a same form.
- `Where`: a method for filtering or slicing by condition. See [Where](../Object Manipulation/Where.md)
- `ForEach`: a method to perform iteration with certain logic or perform casting all items to target type. See [ForEach](../Object Manipulation/ForEach.md)
- `Count`
- `Length`


> [!NOTE]
>**Intrinsic members are not described as part of certain type definition, they're isolated from the object-oriented type system.**
> Object views are visible to `Get-Member -Force` while intrinsic methods aren't.
A common example would be `Process` type.

Expand All @@ -16,18 +54,16 @@ A common example would be `Process` type.
(gps | select -First 1) -is [System.Diagnostics.Process] # True
```

The functionality of ETS achieved benifits:
The functionality of ETS achieved benefits:

- Allow custom behaviours like formatting, sorting for your custom type or object.
- Allow custom behaviors like formatting, sorting for your custom type or object.
- Dynamic interpreting a `HashTable` to any `PSObject`
- Accessibility to the underlying object.
- Manipulation on extended members.

## What's `PSObject`

## ETS Members

To represent meta of ETS members, Powershell has an abstract class called `PSMemberInfo`, each concrete member info type is derived from it.
To represent meta of ETS members, PowerShell has an abstract class called `PSMemberInfo`, each concrete member info type is derived from it.

```cs
public abstract class PSMemberInfo { }
Expand All @@ -37,4 +73,3 @@ public abstract class PSMemberInfo { }
The facts of ETS members:
- each member has a type inherited from `PSMemberInfo`.

## PSObject

0 comments on commit 49a0ea8

Please sign in to comment.