2017-05-24 5 views
2

特定の行の前にファイル/テンプレートに行を追加するにはどうすればよいですか?Inno Setup - 特定の行の前にあるテキストファイル/テンプレートに行を挿入します(存在しない場合)

たとえば、次のJSファイルの場合、とBELOW THIS LINEのコメント行の間にdependencies.push(...)行があることを確認する必要があります。 dependencies.push(...)が存在しない場合、私はBELOW THIS LINEコメント行の前にそれを追加する必要があります。

(function(ng) { 
    var dependencies = []; 

    /*DO NOT MODIFY ABOVE THIS LINE!*/ 

    dependencies.push("mxdfNewTransaction.controller.mxdfNewTransactionCtrl"); 

    /*DO NOT MODIFY BELOW THIS LINE!*/ 

    ng.module('prismApp.customizations', dependencies, null); 
})(angular); 

私も似たHTMLテンプレートファイルと同じことを行う必要があります。

ありがとうございました。

+0

"指定された他の行" - 指定方法は?私たちに例を示してください。 –

+0

@ MartinPrikryl私は私の質問を編集しました。理解する方が良いかもしれません。 –

+0

私はまだ理解していない、あなたが望むもの。既存の 'customizations.js'ファイルを変更していますか?または、インストーラによってファイルが完全に作成されていますか?最初の場合は、インストール前にファイルがどのように見えるのですか。 –

答えて

1

コードを挿入する場所を見つけるために、ファイルを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のファイルを扱う場合、LoadStringsFromFileSaveStringsToFile制限に注意してください。 Inno Setup Reading file in Ansi and Unicode encodingを参照してください。

関連する問題