替换深度嵌套层次中的版权标题

替换深度嵌套层次中的版权标题

我们的项目是开源的,我需要用新许可证替换所有版权标头。该项目包含嵌套文件夹层次结构中的大约 1500 个 C++/Obj-C/Java 文件。

标题跨越 1 到 5 行,且格式不同,因此正则表达式不能保证在所有行中找到它。

您会采取什么方法呢?

例子:

C++ 文件:

/*******************************************************************************
 * Copyright 1996: Börk Börk Inc. All Rights
 * Reserved. Proprietary and Confidential information of BOBO. Disclosure,
 * Use, or Reproduction without written authorization of BOBO is prohibited.
 *******************************************************************************/
#ifndef Things_cpp
#define Things_cpp

#include <LibOne.hpp>
#include <LibTwo.hpp>


Namespacington::ClassName::HereBeMethod(void)
{
}
#endif

Java 文件:

package com.bork.bork.boooork;

/*******************************************************
 * Copyright 1996: Börk Börk Inc. All Rights Reserved.
 * Proprietary and Confidential information of BOBO. 
 * Disclosure, Use, or Reproduction without written 
 * authorization of BOBO is prohibited.
 *******************************************************

import java.util.List;

/**
 * <p>
 * Callback interface/protocol for a proxy factory.
 * </p>
 */

@SuppressWarnings("all")
public interface ProxyFactorize
{
    /**
     * <p>
     * Do the thing
     * </p>
     * @param bork Spices
     * @param borkbork Condiments
     */

    void apply(double bork, double borkbork);

}

Obj-C 文件:

/*******************************************************
 * Copyright 1996: Börk Börk Inc.
 * All Rights Reserved.
 * Proprietary and Confidential information of BOBO. 
 * Disclosure, Use, or Reproduction without written 
 * authorization of BOBO is prohibited.
 ********************************************************/

#import <Bork/Booork.h>
@class Biorjk;

/**
 * Bork bork bork booooork
 *
 * @warning Bork?
 *
 * @warning Bork
 *
 */
@interface Biorjk : Borkburk

@end

新的许可证将应用于所有文件:

/* Copyright 2015 Bork Bork
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

答案1

如果标题差异太大,您甚至无法确定是否可以使用正则表达式来捕获所有标题,那么您需要最强大的工具:正则表达式。只需签出所有文件,对子文件夹中的所有文件执行基于正则表达式的替换即可。然后检查哪些文件尚未修改,查看其中的一些标题,使用调整后的正则表达式替换这些标题。重复此操作,直到完成。完成后,进行一次签入。

这是唯一的办法。

一个简单丑陋的一次性正则表达式,适用于 3 个示例文件中的 2 个,如下所示:

[^\{\}\(\)\/]{0,200}(\/\*[*\s]*[Cc]opyright[^\n\r]*Börk Börk[a-zA-Z0-9\s\*,.]*\*\/)

您可以在这里测试:https://regex101.com/

它与第二个文件不匹配,因为该文件格式很糟糕,版权注释没有结束,而是意外地包含了导入语句。

有很多工具可以跨多个文件执行正则表达式替换,只需使用其中任何一个即可。

答案2

我曾经用过http://code.mycila.com/license-maven-plugin/将许可证标头添加到我的源文件中。(我不需要删除现有的许可证,但插件文档说它也可以删除标头。)

相关内容