Skip to content

Commit 6a6d7a5

Browse files
committed
Generate PowerShell various fixes
Generate PowerShell script, fixes for fixed width and additional examples and comments
1 parent f4b936d commit 6a6d7a5

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

CSVLintNppPlugin/CsvLint/CsvGenerateCode.cs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,8 @@ public static void GeneratePowerShell(CsvDefinition csvdef)
769769

770770
var col_names = "";
771771
var col_fixed = "";
772+
var col_fixed_write1 = "";
773+
var col_fixed_write2 = "";
772774
var col_order = "";
773775
var col_types = "";
774776
var col_enums = "";
@@ -800,7 +802,8 @@ public static void GeneratePowerShell(CsvDefinition csvdef)
800802

801803
// list all column names
802804
col_names += string.Format("\"{0}\"{1}", coldef.Name, comma);
803-
col_order += string.Format("\t\t{0} = $_.{1}\r\n", colnamepad, colname);
805+
var datemask = (coldef.DataType == ColumnType.DateTime ? string.Format(".ToString(\"{0}\")", coldef.Mask) : "");
806+
col_order += string.Format("\t\t{0} = $_.{1}{2}\r\n", colnamepad, colname, datemask);
804807

805808
// enumeration
806809
if (coldef.isCodedValue)
@@ -855,6 +858,8 @@ public static void GeneratePowerShell(CsvDefinition csvdef)
855858
col_fixed += string.Format("\t\t{0} = $line.Substring({1}, {2}).Trim(' \"')\r\n", colnamepad, strpos, strwid);
856859
startpos += coldef.MaxWidth;
857860
}
861+
col_fixed_write1 += string.Format("{{{0},{1}{2}}} ", c, (coldef.DataType == ColumnType.Integer || coldef.DataType == ColumnType.Decimal ? "" : "-"), coldef.MaxWidth);
862+
col_fixed_write2 += string.Format("$row.{0}{1}", colname, comma);
858863
}
859864

860865
// no decimals, then not technically needed but nice to have as example code
@@ -884,13 +889,26 @@ public static void GeneratePowerShell(CsvDefinition csvdef)
884889
if (csvdef.Separator == '\0')
885890
{
886891
// fixed width
887-
ps1.Append(string.Format("# read fixed width data file, positions {0}\r\n", GetColumnWidths(csvdef, true)));
888-
892+
ps1.Append(string.Format("# read fixed width data file, positions {0}\r\n", csvdef.GetColumnWidths(true)));
889893
ps1.Append("$stream_in = [System.IO.StreamReader]::new($filename)\r\n\r\n");
894+
895+
if (csvdef.SkipLines > 0)
896+
{
897+
ps1.Append(string.Format("# skip first {0} lines\r\n", csvdef.SkipLines));
898+
ps1.Append(string.Format("for ($i=0; $i -lt {0}; $i=$i+1) { $skipline = $stream_in.ReadLine() }\r\n", csvdef.SkipLines));
899+
}
900+
901+
if (csvdef.ColNameHeader)
902+
{
903+
ps1.Append("# skip header\r\n");
904+
ps1.Append("$skipline = $stream_in.ReadLine()\r\n");
905+
}
906+
907+
ps1.Append("# read fixed width data\r\n");
890908
ps1.Append("$csvdata = while ($line = $stream_in.ReadLine()) {\r\n");
891909
ps1.Append("\t[PSCustomObject]@{\r\n");
892910
ps1.Append(col_fixed);
893-
ps1.Append("\t}\r\n}\r\n\r\n");
911+
ps1.Append("\t}\r\n}\r\n$stream_in.Dispose()\r\n\r\n");
894912
}
895913
else
896914
{
@@ -957,13 +975,22 @@ public static void GeneratePowerShell(CsvDefinition csvdef)
957975
ps1.Append("\t}\r\n");
958976
ps1.Append("}\r\n\r\n");
959977

960-
ps1.Append("# Merge datasets example, to join on multiple columns use a list, for example: on=['patient_id', 'center_id']\r\n");
961-
ps1.Append("# $merged_df = Join-Object -Left $PSCustomObject -Right $DataTable -LeftJoinProperty 'ID' -RightJoinProperty 'IDD' -ExcludeRightProperties 'Junk' -Prefix 'R_' | Format-Table # same key column name\r\n");
962-
ps1.Append("# $merged_df = Join-Object -Left $PSCustomObject -Right $DataTable -LeftJoinProperty 'ID' -RightJoinProperty 'IDD' -ExcludeRightProperties 'Junk' -Prefix 'R_' | Format-Table # different key column names\r\n\r\n");
978+
ps1.Append("## Merge datasets in PowerShell requires custom external modules which goes beyond the scope of this generated script\r\n");
979+
ps1.Append("##Install-Module -Name Join-Object\r\n");
980+
ps1.Append("##$merged_df = Join-Object -Left $patients -Right $visits -LeftJoinProperty 'PATIENT_ID' -RightJoinProperty 'PATIENT_ID' -ExcludeRightProperties 'Junk' -Prefix 'R_' | Format-Table\r\n\r\n");
963981

964982
ps1.Append("# csv write new output\r\n");
965983
ps1.Append("$filenew = $pathname + \"output.txt\"\r\n");
966-
ps1.Append(string.Format("$csvnew | Export-Csv -Path $filenew -Delimiter \"`t\" -NoTypeInformation\r\n", separator));
984+
ps1.Append(string.Format("$csvnew | Export-Csv -Path $filenew -Encoding utf8 -Delimiter \"`t\" -NoTypeInformation\r\n\r\n", separator));
985+
986+
ps1.Append("# alternatively, write as fixed width\r\n");
987+
ps1.Append("#$stream_out = New-Object System.IO.StreamWriter $filenew\r\n");
988+
ps1.Append("#foreach ($row in $csvnew)\r\n");
989+
ps1.Append("#{\r\n");
990+
ps1.Append("#\t# {colnr,width} space etc, negative width means left aligned\r\n");
991+
ps1.Append(string.Format("#\t$stream_out.WriteLine((\"{0}\" -f {1}))\r\n", col_fixed_write1.Trim(), col_fixed_write2));
992+
ps1.Append("#}\r\n");
993+
ps1.Append("#$stream_out.Dispose()\r\n");
967994

968995
// create new file
969996
notepad.FileNew();
@@ -974,4 +1001,4 @@ public static void GeneratePowerShell(CsvDefinition csvdef)
9741001
}
9751002
}
9761003
}
977-
}
1004+
}

0 commit comments

Comments
 (0)