File: sort_solution

package info (click to toggle)
pmdk 1.13.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 28,944 kB
  • sloc: ansic: 126,815; sh: 21,543; cpp: 9,413; python: 5,893; makefile: 3,119; perl: 2,294; pascal: 1,442
file content (128 lines) | stat: -rwxr-xr-x 2,668 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/perl
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2016, Intel Corporation

#
# sort_solution -- sort visual studio solution projects lists
#

use strict;
use warnings;
# install libtext-diff-perl or perl-Text-Diff
use Text::Diff;

use Cwd 'abs_path';
use File::Basename;
use File::Compare;


sub help {
	print "Usage: sort_solution [check|sort]\n";
	exit;
}

sub sort_global_section {
	my ($solution_fh, $temp_fh, $section_name) = @_;
	my $line = "";
	my @array;

	while (defined($line = <$solution_fh>) && ($line !~ $section_name)) {
		print $temp_fh $line;
	}
	print $temp_fh $line;

	while (defined($line = <$solution_fh>) && ($line !~ "EndGlobalSection")) {
		push @array, $line;
	}

	@array = sort @array;

	foreach (@array) {
		print $temp_fh $_;
	}

	print $temp_fh $line; # print EndGlobalSection line
}

my $num_args = $#ARGV + 1;
if ($num_args != 1) {
	help;
}
my $arg = $ARGV[0];

if($arg ne "check" && $arg ne "sort") {
	help;
}
my $filename = dirname(abs_path($0)).'/../src/PMDK.sln';
my $tempfile = dirname(abs_path($0)).'/../src/temp.sln';

open(my $temp_fh, '>', $tempfile)
	or die "Could not open file '$tempfile' $!";
open(my $solution_fh, '<:crlf', $filename)
	or die "Could not open file '$filename' $!";

my $line;

# Read a header of file
while (defined($line = <$solution_fh>) && ($line !~ "^Project")) {
	print $temp_fh $line;
}

my @part1;
my $buff;
my $guid;

# Read the projects list with project dependencies
do {
	if($line =~ "^Project") {
		$buff = $line;
		$guid = (split(/\,/, $line))[2];
	} elsif($line =~ "^EndProject") {
		$buff .= $line;
		my %table = (
			guid => $guid,
			buff => $buff,
		);
		push @part1, \%table;
	} else {
		$buff .= $line;
	}

} while (defined($line = <$solution_fh>) && $line ne "Global\n");

# sort the project list by a project GIUD and write to the tempfile
@part1 = sort { $a->{guid} cmp $b->{guid} } @part1;

foreach (@part1) {
	my %hash = %$_;
	print $temp_fh $hash{"buff"};
}
print $temp_fh $line; # EndProject line

sort_global_section $solution_fh, $temp_fh, "ProjectConfigurationPlatforms";

sort_global_section $solution_fh, $temp_fh, "NestedProjects";

# read solution file to the end and copy it to the temp file
while (defined($line = <$solution_fh>)){
	print $temp_fh $line;
}

close($temp_fh);
close($solution_fh);

if($arg eq "check") {
	my $diff = diff $filename => $tempfile;
	if ($diff eq "") {
		unlink $tempfile;
		exit;
	}

	print "PMDK solution file is not sorted, " .
		"please use sort_solution script before pushing your changes\n";
	unlink $tempfile;
	exit 1;
} else {
	unlink $filename or die "Cannot replace solution file $!";
	rename $tempfile, $filename;
}