<commit_before><commit_msg>make2.ps1: new build script (under the construction)<commit_after>Set-PSDebug -strict

Function Get-GoArch{
    if( Test-Path "goarch.txt" ){
        (Get-Content "goarch.txt")
    }else{
        ( go version | %{ $_.Split()[-1].Split("/")[-1] })
    }
}

Function Get-Version{
    Get-Content Misc\version.txt
}

Function Get-Commit{
    git describe --tags
}

Function EachGoDir{
    Get-ChildItem . -Recurse | 
    ?{ $_.Extension -eq '.go' } |
    %{ [System.IO.Path]::GetDirectoryName($_.FullName)} |
    Sort-Object |
    Get-Unique
}

Function Get-Imports {
    Get-ChildItem . -Recurse |
    ?{ $_.Extension -eq '.go' } |
    %{ Get-Content $_.FullName  } |
    %{ ($_ -replace '\s*//.*$','').Split()[-1] <# remove comment #>} |
    ?{ $_ -match 'github.com/' } |
    ?{ -not ($_ -match '/nyagos/') } |
    %{ $_ -replace '"','' } |
    Sort-Object |
    Get-Unique
}

function Get-Modified {
    Get-ChildItem . -Recurse -Name |
    ?{ $_ -like "*.go" } |
    ?{ 
        $private:attr = [System.IO.File]::GetAttributes($_)
        ($attr -band [System.IO.FileAttributes]::Archive) -eq
            [System.IO.FileAttributes]::Archive
    }
}

function Set-NoModified($path) {
    $private:attr = [System.IO.File]::GetAttributes($path)
    [System.IO.File]::SetAttributes($path,
        $attr -band -bnot [System.IO.FileAttributes]::Archive)
}

function Go-Fmt{
    Get-Modified | %{ go fmt -n $_ ; Set-NoModified($_) }
}

function Get-Go1stPath {
    if( $env:gopath -ne "" ){
        $gopath = $env:gopath
    }else{
        $gopath = Join-Path($env:userprofile,"go")
    }
    $gopath.Split(";")[0]
}

function Make-JSON($version){
    $version = [string]$version
    $json = @{
        FixedFileInfo = @{
            FileFlagsMask="3f";
            FileFlags="00";
            FileOS="040004";
            FileType="01";
            FileSubType="00";
        };
        StringFileInfo = @{
            Comments= "";
            CompanyName= "NYAOS.ORG";
            FileDescription= "Extended Commandline Shell";
            InternalName= "";
            LegalCopyright= "Copyright (C) 2014-2017 HAYAMA_Kaoru";
            LegalTrademarks= "";
            OriginalFilename= "NYAGOS.EXE";
            PrivateBuild= "";
            ProductName= "Nihongo Yet Another GOing Shell";
            SpecialBuild= "";
        };
        VarFileInfo = @{
            Translation=@{
                LangID= "0411";
                CharsetID= "04E4";
            }
        }
    }
    if( $version -like "*.*.*_*" ){
        $v = $version -split "[\._]"
    }else{
        $v = @(0,0,0,0)
    }
    $private:versionSet = @{
        Major = $v[0];
        Minor = $v[1];
        Patch = $v[2];
        Build = $v[3];
    }
    $json["FixedFileInfo"]["FileVersion"] = $versionSet
    $json["FixedFileInfo"]["ProductVersion"] = $versionSet
    $json["StringFileInfo"]["FileVersion"] = $version
    $json["StringFileInfo"]["ProductVersion"] = $version

    $json | ConvertTo-Json
}

function Go-VersionInfo($version) {
    $cwd = Get-Location
    if( -not (Test-Path("goversioninfo.exe")) ){
        go get "github.com/josephspurrier/goversioninfo"
        $gopath1 = Get-Go1stPath
        pushd (Join-Path $gopath1 "src\github.com\josephspurrier\goversioninfo\cmd\goversioninfo")
        go build
        copy goversioninfo.exe $cwd
        popd
    }
    Make-JSON( $version ) | Set-Content -encoding UTF8 Misc\version.json
    goversioninfo.exe -icon mains\nyagos.ico -o nyagos.syso Misc\version.json
}

function Make-Bindata {
    Get-ChildItem .\nyagos.d -Recurse |
    

}

function Do-Build {

}


switch( $args[0] ){
    "" {
        Write-Host("goarch=" + (Get-GoArch))
        Write-Host("version=" + (Get-Version))
        Write-Host("commit=" + (Get-Commit))
        EachGoDir | %{ Write-Host $_ } 
        Get-Imports | %{ Write-Host $_ }
    }
    "get" {
        Get-Imports | %{ Write-Host $_ ; go get -u $_ }
    }
    "fmt" {
        Go-Fmt
    }
    "t" {
        Go-VersionInfo
        #Make-JSON("1.2.3_4")
    }
}
{'fraction_non_alphanumeric': 0.17278777421740202, 'fraction_numerical': 0.012817352723687454, 'mean_word_length': 2.0835866261398177, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4275688', 'n_tokens_mistral': 1437, 'n_tokens_neox': 1395, 'n_words': 234}