File indexing completed on 2024-05-05 04:00:57

0001 #!/usr/bin/env ruby
0002 # frozen_string_literal: true
0003 #
0004 # Copyright (C) 2016 Harald Sitter <sitter@kde.org>
0005 #
0006 # This library is free software; you can redistribute it and/or
0007 # modify it under the terms of the GNU Lesser General Public
0008 # License as published by the Free Software Foundation; either
0009 # version 2.1 of the License, or (at your option) version 3, or any
0010 # later version accepted by the membership of KDE e.V. (or its
0011 # successor approved by the membership of KDE e.V.), which shall
0012 # act as a proxy defined in Section 6 of version 3 of the license.
0013 #
0014 # This library is distributed in the hope that it will be useful,
0015 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0016 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0017 # Lesser General Public License for more details.
0018 #
0019 # You should have received a copy of the GNU Lesser General Public
0020 # License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0021 
0022 require 'pathname'
0023 require 'pp'
0024 
0025 class Origin
0026   attr_accessor :filetype
0027 
0028   def initialize(pathname)
0029     @pathname = pathname
0030     @filetype = '.png'
0031   end
0032 
0033   def size
0034     name = @pathname.dirname.basename
0035     raise "found scalable link, can't process" if name == 'scalable'
0036     name
0037   end
0038 
0039   def basename
0040     @pathname.basename.to_s.gsub('.svg', @filetype)
0041   end
0042 
0043   def type
0044     @pathname.dirname.dirname.basename
0045   end
0046 
0047   def relative_dir
0048     return "#{size}x#{size}/#{type}" if filetype == '.png'
0049     "scalable/#{type}"
0050   end
0051 
0052   def absolute_dir
0053     File.absolute_path(relative_dir)
0054   end
0055 
0056   def relative_path
0057     "#{relative_dir}/#{basename}"
0058   end
0059 
0060   def absolute_path
0061     File.absolute_path(relative_path)
0062   end
0063 end
0064 
0065 class Symlink
0066   attr_reader :origin
0067   attr_reader :target
0068   attr_reader :real_target
0069 
0070   def initialize(origin)
0071     origin = File.absolute_path(origin)
0072     target = File.readlink(origin)
0073     @origin = Pathname.new(origin)
0074     @target = Pathname.new(target)
0075     @real_target = @target.realpath(@origin.dirname) # resolve
0076   end
0077 
0078   def pixel_compatible?
0079     same_size?
0080   end
0081 
0082   def same_size?
0083     @origin.dirname.basename == @real_target.dirname.basename
0084   end
0085 end
0086 
0087 files = Dir.glob('../breeze-icons/icons/**/**')
0088 files.reject! { |file| !File.symlink?(file) }
0089 files.uniq!
0090 files.compact!
0091 files.collect! { |file| File.absolute_path(file) }
0092 
0093 rejected = 0
0094 symlinks = files.collect { |file| Symlink.new(file) }
0095 symlinks.reject! do |symlink|
0096   reject = !symlink.pixel_compatible?
0097   if reject
0098     puts 'cannot replicate symlink because the size is different between the link parts'
0099     puts '----'
0100     puts symlink.inspect
0101     rejected += 1
0102   end
0103   reject
0104 end
0105 
0106 missing_targets = 0
0107 existing_symlinks = 0
0108 existing_files = 0
0109 
0110 symlinks.each do |symlink|
0111   origin = Origin.new(symlink.origin)
0112   target = Origin.new(symlink.real_target)
0113 
0114   unless File.exist?(target.absolute_path)
0115     warn "#{target.relative_path} does not exist"
0116     warn "wanted to link #{origin.relative_path} to it"
0117     puts '----'
0118     missing_targets += 1
0119     next
0120   end
0121 
0122   link_target = Pathname.new(target.absolute_path).relative_path_from(Pathname.new(origin.absolute_dir))
0123   Dir.chdir(origin.relative_dir) do
0124     if File.symlink?(origin.basename)
0125       existing_symlinks += 1
0126       next
0127     end
0128     if File.file?(origin.basename)
0129       existing_files += 1
0130       next
0131     end
0132     File.symlink(link_target, origin.basename)
0133   end
0134 end
0135 
0136 puts "rejected: #{rejected} (because of size mismatch between target and source)"
0137 puts "accepted: #{symlinks.size} (tried to link this many files)"
0138 puts "missing: #{missing_targets} (where we wanted to link to was missing)"
0139 puts "existing link: #{existing_symlinks} (these symlinks existed already)"
0140 puts "existing file: #{existing_files} (these files existed already)"
0141 puts "replicated: #{symlinks.size - missing_targets - existing_symlinks - existing_files} (total)"
0142 # pp symlinks