Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

Change culture (locale) of current PowerShell process

You can change culture (locale) into English with chcp 437 in Windows 7.

In contrast, You cannot change culture with chcp in Windows 10.

In Windows 10, if you want to execute single PowerShell script in another culture, you can execute the script like this:

[Threading.Thread]::CurrentThread.CurrentUICulture = 'en-US'; .\myscript.ps1

If you want to change culture throughout every command in a PowerShell process, you have to change the culture setting cached in PowerShell runtime with reflection like this:

# example: Set-PowerShellUICulture -Name "en-US"

function Set-PowerShellUICulture {
    param([Parameter(Mandatory=$true)]
          [string]$Name)

    process {
        $culture = [System.Globalization.CultureInfo]::CreateSpecificCulture($Name)
       
        $assembly = [System.Reflection.Assembly]::Load("System.Management.Automation")
        $type = $assembly.GetType("Microsoft.PowerShell.NativeCultureResolver")
        $field = $type.GetField("m_uiCulture", [Reflection.BindingFlags]::NonPublic -bor [Reflection.BindingFlags]::Static)
        $field.SetValue($null, $culture)
    }
}

Note: Some cmdlets like Add-Type are not affected this setting.

(from https://gist.github.com/sunnyone/7486486 via https://twitter.com/altrive/status/401349992536756224?s=20)

Import assemblies for C# embedded in PowerShell

You can import assemblies (.NET DLLs) in PowerShell like this:

[void][reflection.assembly]::LoadWithPartialName("System.Drawing")
New-Object System.Drawing.Drawing2D.GraphicsPath

But you will get an error when you tring to use these assemblies in C# source embedded in PowerShell script. Assemblies loaded with LoadWithPartialName aren't referenced from embedded source.

[void][reflection.assembly]::LoadWithPartialName("System.Drawing")
Add-Type @"
using System.Drawing.Drawing2D;
"@
Add-Type : c:\tmp\_System\ama4povk.0.cs(1) : The type or namespace name 'Drawing2D' could not be found (are you missing a using directive or an assembly reference?)
c:\tmp\_System\ama4povk.0.cs(1) : >>> using System.Drawing.Drawing2D;
At line:1 char:1
+ Add-Type @"
+ ~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Exception
    + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

Add-Type : Cannot add type. Compilation errors occurred.
At line:1 char:1
+ Add-Type @"
+ ~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Add-Type], InvalidOperationException
    + FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand

With Add-Type, you have to specify assemblies with -ReferencedAssemblies option like this:

Add-Type -ReferencedAssemblies ("System.Drawing") @"
using System.Drawing.Drawing2D;
"@

mvn command results in error in Windows

You will get error when you run following Maven command from Windows PowerShell:

PS C:\workspace> mvn -B archetype:generate -DgroupId=com.example -DartifactId=hajiboot -Dversion=1.0.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-quickstart
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.069 s
[INFO] Finished at: 2019-11-14T23:51:13+09:00
[INFO] ------------------------------------------------------------------------
[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (C:\home\satob\workspace). Please verify you invoked Maven from the correct directory. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MissingProjectException

You will see the following message with -e -X options:

PS C:\workspace> mvn -B archetype:generate -DgroupId=com.example -DartifactId=hajiboot -Dversion=1.0.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-quickstart -e -X
Apache Maven 3.6.2 (40f52333136460af0dc0d7232c0dc0bcf0d9e117; 2019-08-28T00:06:16+09:00)
Maven home: C:\opt\maven\bin\..
Java version: 1.8.0_212, vendor: AdoptOpenJDK, runtime: C:\opt\java\AdoptOpenJDK\8\jre
(snip)
[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (C:\home\satob\workspace). Please verify you invoked Maven from the correct directory. -> [Help 1]
org.apache.maven.lifecycle.MissingProjectException: The goal you specified requires a project to execute but there is no POM in this directory (C:\home\satob\workspace). Please verify you invoked Maven from the correct directory.
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:85)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
[ERROR]
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MissingProjectException

To solve this problem, quote all options like:

PS C:\workspace> mvn -B archetype:generate "-DgroupId=com.example" "-DartifactId=hajiboot" "-Dversion=1.0.0-SNAPSHOT" "-DarchetypeArtifactId=maven-archetype-quickstart"

cf. https://stackoverflow.com/a/11199865/3902663

「大規模システム」の指す範囲

「100万行オーバーのモノリシックRailsアプリをマイクロサービス化したクックパッドの手順」*1では、100万行弱のコード(うちテストコードが約51万行、実行環境で動作していたのは約41万行)をもって「世界一のモノリシック」「巨大すぎる」と形容している。

また、GoogleBigtableの開発には「7人年(=84人月)の工数がかけられた」*2ということをもって、大きな工数がかかっていることが形容されている。

一方で、「みずほ35万人月」*3とか「郵政民営化1億ステップ」*4のように、SIの世界では文字通り桁の違う数字が「大規模プロジェクト」の例として挙げられる。

この感覚の差はどこから来るのだろう? ドメインモデルスタイルで設計してるシステムと、トランザクションスクリプトスタイルで設計してるシステムとの違いなのかな?