コードを挿入する場所を見つけるために、ファイルを1行ずつ解析する必要があります。このような
何か:
function AddLineToTemplate(
FileName: string; StartLine, EndLine, AddLine: string): Boolean;
var
Lines: TArrayOfString;
Count, I, I2: Integer;
Line: string;
State: Integer;
begin
Result := True;
if not LoadStringsFromFile(FileName, Lines) then
begin
Log(Format('Error reading %s', [FileName]));
Result := False;
end
else
begin
State := 0;
Count := GetArrayLength(Lines);
for I := 0 to Count - 1 do
begin
Line := Trim(Lines[I]);
if (CompareText(Line, StartLine) = 0) then
begin
State := 1;
Log(Format('Start line found at %d', [I]));
end
else
if (State = 1) and (CompareText(Line, AddLine) = 0) then
begin
Log(Format('Line already present at %d', [I]));
State := 2;
break;
end
else
if (State = 1) and (CompareText(Line, EndLine) = 0) then
begin
Log(Format('End line found at %d, inserting', [I]));
SetArrayLength(Lines, Count + 1);
for I2 := Count - 1 downto I do
Lines[I2 + 1] := Lines[I2];
Lines[I] := AddLine;
State := 2;
if not SaveStringsToFile(FileName, Lines, False) then
begin
Log(Format('Error writting %s', [FileName]));
Result := False;
end
else
begin
Log(Format('Modifications saved to %s', [FileName]));
end;
break;
end;
end;
if Result and (State <> 2) then
begin
Log(Format('Spot to insert line was not found in %s', [FileName]));
Result := False;
end;
end;
end;
あなたはこのようにそれを使用することができます:
if AddLineToTemplate(
'C:\path\to\customizations.js',
'/*DO NOT MODIFY ABOVE THIS LINE!*/',
'/*DO NOT MODIFY BELOW THIS LINE!*/',
' dependencies.push("mxdfNewTransaction.controller.mxdfNewTransactionCtrl");') then
begin
Log('Success');
end
else
begin
Log('Failure');
end;
Unicodeのファイルを扱う場合、LoadStringsFromFile
とSaveStringsToFile
制限に注意してください。 Inno Setup Reading file in Ansi and Unicode encodingを参照してください。
"指定された他の行" - 指定方法は?私たちに例を示してください。 –
@ MartinPrikryl私は私の質問を編集しました。理解する方が良いかもしれません。 –
私はまだ理解していない、あなたが望むもの。既存の 'customizations.js'ファイルを変更していますか?または、インストーラによってファイルが完全に作成されていますか?最初の場合は、インストール前にファイルがどのように見えるのですか。 –