Revision: 9757 https://osdn.net/projects/ttssh2/scm/svn/commits/9757 Author: zmatsuo Date: 2022-02-19 23:34:01 +0900 (Sat, 19 Feb 2022) Log Message: ----------- Subversion の出力がローカライズされていても正しく動作するよう修正 - svn info の出力が英語以外の場合を考慮していなかった - svn info --xml の出力を使用するようにした - スクリプトをリファクタリングした - use strict; - use warnings; - ファイル書き出し部分を関数に分離した - Subversion for Windows の記述を追加 - doc/{ja|en}/html/reference/develop.html の記述に合わせた Modified Paths: -------------- trunk/svnrev_perl/readme.md trunk/svnrev_perl/svnrev.pl -------------- next part -------------- Modified: trunk/svnrev_perl/readme.md =================================================================== --- trunk/svnrev_perl/readme.md 2022-02-18 23:46:09 UTC (rev 9756) +++ trunk/svnrev_perl/readme.md 2022-02-19 14:34:01 UTC (rev 9757) @@ -1,9 +1,12 @@ # svnrev -- svn(又は git)の情報からヘッダファイルとバッチファイルを作成する - - `../teraterm/ttpdlg/svnversion.h` - - `sourcetree_info.bat` - - buildフォルダの `build_config.cmake` (cmake時のみ) +- ソースツリーのsvn(又は git)の情報をヘッダファイル等に書き出すためのツール + - ソース用のヘッダフィル + - 例 `../teraterm/ttpdlg/svnversion.h` + - CI用のbatファイル + - 例 `sourcetree_info.bat` + - cmake用ファイル + - 例 buildフォルダの `build_config.cmake` ## 準備 @@ -16,6 +19,8 @@ - perl が見つからない場合は svnversion.default.h が使用される - svn(又は git)をインストールしておく - Windows用svnの例 + - Subversion for Windows + - http://sourceforge.net/projects/win32svn/ - TortoiseSVN の command line client tools - https://tortoisesvn.net/ - Windows用gitの例 Modified: trunk/svnrev_perl/svnrev.pl =================================================================== --- trunk/svnrev_perl/svnrev.pl 2022-02-18 23:46:09 UTC (rev 9756) +++ trunk/svnrev_perl/svnrev.pl 2022-02-19 14:34:01 UTC (rev 9757) @@ -1,19 +1,24 @@ -use utf8; +use strict; +use warnings; +use utf8; use Getopt::Long 'GetOptions'; use POSIX 'strftime'; + binmode STDOUT, ':encoding(utf8)'; -$version = "5.0"; -$svn = "svn"; -$git = "git"; -$out_header = "svnversion.h"; -$out_bat = "sourcetree_info.bat"; -$out_cmake = ""; -$source_root = ".."; -$date = strftime "%Y%m%d", localtime; -$time = strftime "%H%M%S", localtime; -$verbose = 0; -$script_name = $0; +my $version = "5.0"; +my $svn = "svn"; +my $git = "git"; +my $out_header = "svnversion.h"; +my $out_bat = "sourcetree_info.bat"; +my $out_cmake = ""; +my $source_root = ".."; +my $date = strftime "%Y%m%d", localtime; +my $time = strftime "%H%M%S", localtime; +my $verbose = 0; +my $script_name = $0; +my $header = "This file was generated by svnrev_perl/svnrev.pl"; +my %svninfo = (); sub read_toolinfo { my $info = "toolinfo.txt"; @@ -23,7 +28,7 @@ open(my $FD, "<:utf8:crlf", $info); while (my $l = <$FD>) { chomp $l; - $str =~ s/^\x{FEFF}//; # remove BOM + $l =~ s/^\x{FEFF}//; # remove BOM if ($l =~ /^#/) { next; } @@ -39,6 +44,8 @@ sub search_svn { my @test_list = ( + "C:/Program Files (x86)/Subversion/bin/svn.exe", + "C:/Program Files/Subversion/bin/svn.exe", "C:/Program Files/TortoiseSVN/bin/svn.exe", "C:/cygwin64/bin/svn.exe", "C:/cygwin/bin/svn.exe", @@ -69,6 +76,75 @@ } } +sub dump_info() +{ + my %info = @_; + + print "SVNREVISION $info{'Revision'}\n"; + print "RELEASE $info{'release'}\n"; + print "BRANCH_NAME $info{'name'}\n"; +} + +sub write_info_header +{ + my ($out_header, %svninfo) = @_; + my $revision = $svninfo{'Revision'}; + + open(my $FD, ">$out_header") || die "error $out_header"; + print $FD "/* $header */\n"; + print $FD "/* #define TT_VERSION_STR \"$version\" check teraterm/common/tt-version.h */\n"; + if (defined $revision) { + print $FD "#define SVNVERSION $revision\n"; + } else { + print $FD "#undef SVNVERSION\n"; + } + if ($svninfo{'release'}) { + print $FD "#define TERATERM_RELEASE 1\n"; + } else { + print $FD "#undef TERATERM_RELEASE\n"; + } + print $FD "#define BRANCH_NAME \"$svninfo{'name'}\"\n"; + close($FD); +} + +sub write_info_bat +{ + my ($out_header, %svninfo) = @_; + my $revision = $svninfo{'Revision'}; + + open(my $FD, ">$out_bat") || die "error $out_bat"; + print $FD "\@rem $header\n"; + print $FD "set VERSION=$version\n"; + if (defined $revision) { + print $FD "set SVNVERSION=$revision\n"; + } else { + print $FD "set SVNVERSION=unknown\n"; + } + print $FD "set RELEASE=$svninfo{'release'}\n"; + print $FD "set DATE=$date\n"; + print $FD "set TIME=$time\n"; + close($FD); +} + +sub write_info_cmake +{ + my ($out_header, %svninfo) = @_; + my $revision = $svninfo{'Revision'}; + + open(my $FD, ">$out_cmake") || die "error $out_cmake"; + print $FD "# $header\n"; + print $FD "set(VERSION \"$version\")\n"; + if (defined $revision) { + print $FD "set(SVNVERSION \"$revision\")\n"; + } else { + print $FD "#set(SVNVERSION \"0000\")\n"; + } + print $FD "set(RELEASE $svninfo{'release'})\n"; + print $FD "set(DATE \"$date\")\n"; + print $FD "set(TIME \"$time\")\n"; + close($FD); +} + &search_svn(); &search_git(); &read_toolinfo(); @@ -92,16 +168,14 @@ print "root=$source_root\n"; print "svn=\"$svn\"\n"; print "git=\"$git\"\n"; - print "header=$out_header\n"; - print "bat=$out_bat\n"; - print "cmake=$out_cmake\n"; + print "header=\"$out_header\"\n"; + print "bat=\"$out_bat\"\n"; + print "cmake=\"$out_cmake\"\n"; } -$header = "This file was generated by svnrev_perl/svnrev.pl"; - if (-d "$source_root/.svn" && $svn ne "") { # svn infoを実行、出力をすべて取り込む - if (!open(my $FD, "-|", "\"$svn\" info $source_root 2>&1")) { + if (!open(my $FD, "-|", "\"$svn\" info --xml $source_root 2>&1")) { # svn が実行できない print "$script_name: '$svn' can not execute\n"; } @@ -110,26 +184,28 @@ my $text = do { local $/; <$FD> }; close($FD); - # 必要な情報を取り込む - my @text = split(/[\r\n]+/, $text); - foreach my $l (@text) { - if ($l =~ /Last Changed Rev: (\d+)/) { + # xmlパーサがインストールされていない環境もあるので + # パターンマッチで実装 + if ($text =~ /<commit([^>]+)>/) { + my $commit = $1; + if ($commit =~ /revision=\"(\d+)\"/) { $svninfo{'Revision'} = $1; - } elsif ($l =~ /Relative URL: (.+)$/) { - my $url = $1; - my $name = $url; - $name =~ s/^\^\/(.*)$/$1/; # "\/" を削除する - $svninfo{'name'} = $name; - my $release = 0; - if ($url =~ /tags\/(teraterm-(\d+)_(\d+))/) { - # パス名からリリースと判定 - my $tag = $1; - my $version = "$2.$3"; - $release = 1; - } - $svninfo{'release'} = $release; } } + if ($text =~ /<relative-url>(.+)<\/relative-url>/) { + my $url = $1; + my $name = $url; + $name =~ s/^\^\/(.*)$/$1/; # "\/" を削除する + $svninfo{'name'} = $name; + my $release = 0; + if ($url =~ /tags\/(teraterm-(\d+)_(\d+))/) { + # パス名からリリースと判定 + my $tag = $1; + my $version = "$2.$3"; + $release = 1; + } + $svninfo{'release'} = $release; + } } } elsif(-d "$source_root/.git" && $git ne "") { @@ -142,7 +218,6 @@ $branch =~ s/[\r\n]$//g; $svninfo{'name'} = $branch; - if (-d "$source_root/.git/svn") { # use git svn log my $revision = `\"$git\" svn log --oneline -1`; @@ -170,12 +245,9 @@ if ($svninfo{'release'} != 1) { $svninfo{'release'} = 0; } -local $revision = $svninfo{'Revision'}; if ($verbose != 0) { - print "SVNREVISION $revision\n"; - print "RELEASE $svninfo{'release'}\n"; - print "BRANCH_NAME $svninfo{'name'}\n"; + &dump_info(%svninfo); } # output for source(C,C++) header @@ -182,38 +254,13 @@ if ($verbose != 0) { print "write '$out_header'\n"; } -open(my $FD, ">$out_header") || die "error $out_header"; -print $FD "/* $header */\n"; -print $FD "/* #define TT_VERSION_STR \"$version\" check teraterm/common/tt-version.h */\n"; -if ($revision == undef) { - print $FD "#undef SVNVERSION\n"; -} else { - print $FD "#define SVNVERSION $revision\n"; -} -if ($svninfo{'release'}) { - print $FD "#define TERATERM_RELEASE 1\n"; -} else { - print $FD "#undef TERATERM_RELEASE\n"; -} -print $FD "#define BRANCH_NAME \"$svninfo{'name'}\"\n"; -close($FD); +write_info_header($out_header, %svninfo); # output for bat file if ($verbose != 0) { print "write '$out_bat'\n"; } -open(my $FD, ">$out_bat") || die "error $out_bat"; -print $FD "\@rem $header\n"; -print $FD "set VERSION=$version\n"; -if ($revision == undef) { - print $FD "set SVNVERSION=unknown\n"; -} else { - print $FD "set SVNVERSION=$revision\n"; -} -print $FD "set RELEASE=$svninfo{'release'}\n"; -print $FD "set DATE=$date\n"; -print $FD "set TIME=$time\n"; -close($FD); +write_info_bat($out_bat, %svninfo); # output for cmake if ($out_cmake ne "") { @@ -220,16 +267,5 @@ if ($verbose != 0) { print "write '$out_cmake'\n"; } - open(my $FD, ">$out_cmake") || die "error $out_cmake"; - print $FD "# $header\n"; - print $FD "set(VERSION \"$version\")\n"; - if ($revision == undef) { - print $FD "#set(SVNVERSION \"0000\")\n"; - } else { - print $FD "set(SVNVERSION \"$revision\")\n"; - } - print $FD "set(RELEASE $svninfo{'release'})\n"; - print $FD "set(DATE \"$date\")\n"; - print $FD "set(TIME \"$time\")\n"; - close($FD); + write_info_cmake($out_cmake, %svninfo); }